Mod Rewrite

This is driving me insane. I am trying to use mod rewrite to get from the url:

[noparse]www.mysite.com/folder/?param=1[/noparse]

to:

[noparse]www.mysite.com/folder/1[/noparse]

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?

Thanks!

RewriteEngine On

RewriteBase /
RewriteRule ^/folder/(.*)$ /folder/index.php?param=$1 [NC]

BM,

  1. You don’t need the RewriteBase.

  2. You don’t need (can’t have) the leading / with an Apache 2.x server (it’s required for an Apache 1.x server, though).

  3. No Case is NOT appropriate for a RewriteRule (the {REQUEST_URI} variable IS case sensitive).

  4. Make those mods then test with the flag(s) being [R=301,L] and remove the R=301, when you’re satisfied it works.

Regards,

DK

Hi dklynn,

Thanks for the reply! I have tested what you recommended but im still getting the same error. I have tried the following all with the same result:

RewriteEngine On

RewriteRule ^/folder/(.*)$ /folder/index.php?param=$1 [R=301,L]

RewriteRule ^/folder/(.*)$ folder/index.php?param=$1 [R=301,L]

RewriteRule ^folder/(.*)$ folder/index.php?param=$1 [R=301,L]

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

BM,

(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.

Regards,

DK

Hi dklynn,

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?

BM,

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.

Regards,

DK

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.

BM,

Did you run the test with the code I provided in the tutorial? That will give you a definitive answer.

Regards,

DK

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.

BM,

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.

Regards,

DK

So i finally got it working. I am using the following:


Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)/(.*)/$ /folder/index.php?param1=$1&param2=$2

which converts:

www.mysite.com/folder/index.php?param1=test&param2=1

to:

www.mysite.com/test/1/

The problem was that i had this code beforehand which must have been causing some kind of conflict:


# Remove the file extension from URL's

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]

Off Topic:

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 :slight_smile:

Another day another .htaccess problem :slight_smile:

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&param2=$2


# Remove the file extension from URL's

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]

Thanks!

BM,

[rant #1 REPEATED][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.

Regards,

DK

Thanks again! I’m new to Regex as well so it was all going over my head a bit. I have looked through the regex tutorial and got it working.

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:

www.mywebsite.com/long_complicated_subject_goes_here_1.aspx
www.mywebsite.com/long_complicated_subject_goes_here_1/long_subsection_a_goes here.aspx
www.mywebsite.com/long_complicated_subject_goes_here_1/long_subsection_b_goes here.aspx
www.mywebsite.com/long_complicated_subject_goes_here_1/long_subsection_c_goes here.aspx
www.mywebsite.com/long_complicated_subject_goes_here_1/long_subsection_d_goes here.aspx
www.mywebsite.com/long_complicated_subject_goes_here_1/long_subsection_e_goes here.aspx
www.mywebsite.com/long_complicated_subject_goes_here_1/long_subsection_f_goes here.aspx

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:

www.mywebsite.com/long_complicated_subject_goes_here_1.aspx
www.mywebsite.com/long_complicated_subject_goes_here_1/long_subsection_a_goes here.aspx
www.mywebsite.com/long_complicated_subject_goes_here_1/long_subsection_b_goes here.aspx
www.mywebsite.com/long_complicated_subject_goes_here_1/long_subsection_c_goes here.aspx
www.mywebsite.com/long_complicated_subject_goes_here_1/long_subsection_d_goes here.aspx
www.mywebsite.com/long_complicated_subject_goes_here_1/long_subsection_e_goes here.aspx
www.mywebsite.com/long_complicated_subject_goes_here_1/long_subsection_f_goes here.aspx

www.mywebsite.com/long_complicated_subject_goes_here_2.aspx
www.mywebsite.com/long_complicated_subject_goes_here_2/long_subsection_a_goes here.aspx
www.mywebsite.com/long_complicated_subject_goes_here_2/long_subsection_b_goes here.aspx
www.mywebsite.com/long_complicated_subject_goes_here_2/long_subsection_c_goes here.aspx
www.mywebsite.com/long_complicated_subject_goes_here_2/long_subsection_d_goes here.aspx
www.mywebsite.com/long_complicated_subject_goes_here_2/long_subsection_e_goes here.aspx
www.mywebsite.com/long_complicated_subject_goes_here_2/long_subsection_f_goes here.aspx

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?

FYI — the domain name is not changing.

Thanks,
Dan

BM,

:tup:

Regards,

DK