I have tried out about 10 different mod rewrite version from varios forums but nothing is working. I just keep on getting an Internal Server Error message. Below is one example i have tried but for the life on me can see the problem. Is this code correct? If it is any other ideas what might be causing the issue?
I have other instructions in my .htaccess so maybe they are conflicting somehow?
# redirect non www. to www. domain
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
# Remove the file extension from URL's
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
# server automatically adds a PHP Session ID to URL's so this needs to be switched off
php_flag session.use_trans_sid off
php_flag session.use_only_cookies on
(Not so) silly question first: Is mod_rewrite enabled? (Several ways to check and verify in my signature’s tutorial.)
Your “other” mod_rewrite code is fine.
I’ve got to assume that you tried your new code (after the other mod_rewrite code) individually:
PLEASE wrap your code in [noparse]
...
[/noparse] tags as that preserves the code when quoting for a reply.
RewriteEngine On
RewriteRule ^/folder/(.*)$ /folder/index.php?param=$1 [R=301,L][COLOR="#FF0000"]
# This will ONLY work on an Apache 1.x server (^/) and
# ^ will only work on an Apache 2.x server.
# If you're not sure what you're using, use ^/? as that works with both.
# the / in the redirection is first interpreted as "server root" but should be okay.
# (.*) is a terrible thing to use:
[standard rant #1][indent]The use of "lazy regex," specifically the :kaioken: EVERYTHING :kaioken: atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is "greedy." Unless you provide an "exit" from your redirection, you will ALWAYS end up in a loop![/indent][/standard rant #1]
# In other words, use the !-f test to prevent looping on folder/index.php![/COLOR]
RewriteRule ^/folder/(.*)$ folder/index.php?param=$1 [R=301,L]
[COLOR="#FF0000"]# same ^/ problems (#1 and #3) as above[/COLOR]
RewriteRule ^folder/(.*)$ folder/index.php?param=$1 [R=301,L]
[COLOR="#FF0000"]# Same loopy problem (#3 above).[/COLOR]
# redirect non www. to www. domain
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
[COLOR="#FF0000"]# I prefer to use existing variables rather than create new ones so I use
# RewriteRule .? http://www.mysite.com%{REQUEST_URI} [R=301,L]
# but your RewriteRule is the more common use (by noobies)[/COLOR]
# Remove the file extension from URL's
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
[COLOR="#FF0000"]# I will check whether $1.php exists as a file before making this redirection
[/COLOR]
# server automatically adds a PHP Session ID to URL's so this needs to be switched off
php_flag session.use_trans_sid off
php_flag session.use_only_cookies on
Technique tip: Because Apache’s core directives are executed before mod_rewrite, I will always list them first.
I dont understand what you mean when you say to use the !-f test to prevent looping on folder/index.php?
From your notes above i gather that the below should work then on apache 1 and 2 server, but i should change the (.*)?
RewriteRule ^/folder/(.*)$ /folder/index.php?param=$1 [R=301,L][\\code]
I have tried about 6 different .htaccess code generators all with no luck. There is clearly something fundamental i am doing wrong here but i still cant figure out what?
If mod_rewrite enabled? Have you confirmed that (and that it works on a simple test)? EVERYTHING else would be a waste of time if it’s not enabled for you.
I checked my phpinfo() file but mod_rewrite wasn’t listed. I have been in contact with my hosting company and they assure me that even though it is not listed in my phpinfo() it is turned on on my server.
I tested it and it is working ok. I see the “this is a PHP file” text.
I was wondering is it ok to have the .htaccess file save at my root folder or should it be in the /folder/ as that is what i am trying to effect? I have tried both but not with every test i have tried.
Well, that’s as good a confirmation as you can get! :tup:
As a rule, I keep my .htaccess in the DocumentRoot so I can see all the interactions (conflicts) at a single glance. You CAN do either but that will complicate things unnecessarily for you.
When posting .htaccess files, order matters! So if you have a block A and a block B in your .htaccess file it is wise to also post in which order they are in your .htaccess file.
In this particular post that would have made the problem clear after post #3 (where the OP posts two blocks but it turns out the second one actually came before the first one he posted)
This is not to attack anyone, just to telling the OP and others a way we can better help them
I now have a new problem with this code. If i try to navigate to www.mysite.com/folder2/ i.e. droping the index.php at the end i get a server error. It works if i use the full address i.e. www.mysite.com/folder2/index.php. It seems to be trying to use the new mod rewrite rule i just added even though a different folder has been specified. Any ideas why this is happening?
Options +FollowSymLinks
RewriteEngine on
# redirect non www. to www. domain
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
# server automatically adds a PHP Session ID to URL's so this needs to be switched off
php_flag session.use_trans_sid off
php_flag session.use_only_cookies on
# remove the variables when passed through domain URL
RewriteRule (.*)-(.*)/$ /folder1/index.php?param1=$1¶m2=$2
# Remove the file extension from URL's
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
[rant #1REPEATED][indent]The use of “lazy regex,” specifically the :kaioken: EVERYTHING :kaioken: atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is “greedy.” Unless you provide an “exit” from your redirection, you will ALWAYS end up in a loop![/indent][/rant #1]
Sorry, you are NOT specifying a folder with (.*) but matching EVERYTHING (or NOTHING). That’s been the problem and it’s still the problem.
Once you finally get into your folder2 directory, DirectoryIndex index.php will specify that index.php will be served if only the directory is requested.
Newbie here, trying to map old urls to new urls using rewriterules. I’ve converted a website from DNN to Joomla, and now I’m trying to get my 301 redirects in place before rolling out the new site. I have 7 templates, each which are applied to 22 models. For example, I have:
So each “long_complicated_subject” has 7 pages (landing page and 6 subpages). The subpages for every long_complicated_subject has the same name… like this:
and so on for many “long_complicated_subject” groups. As you can see each long_complicated_subject" has a landing page, and 6 subpages with identical names to every other “long_complicated_subject”. There is no consistency to the actual “long_complicated_subject”, but I’m hoping I don’t have to duplicate the subsections for every “long_complicated_subject”.
Instead of a one-to-one mapping, and ending up with hundreds of rewriterule lines, is there a way to do this in a 2-step process; that is, may I first substitute the “long_complicated_subject” and the once that’s completed, may I do a second substitution on result of the first one?
One more thing, is there a way report the original url and the final url with a 301 back to the search engines?