Forum Moderators: phranque

Message Too Old, No Replies

Rewrite rule to add extension

arg.

         

matthewwithanm

8:31 pm on Jan 25, 2006 (gmt 0)

10+ Year Member



I'm trying to get all requests to 'certainpath/blah' to be redirected to 'otherpath/kid/grandkid/blah.html'.

Could somebody help me out?
Thanks!

jd01

10:05 pm on Jan 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Matthew,

We normally ask people to show us what they have tried, so we can teach them how to write their own code, otherwise, a few of us would end up writing everyone's .htaccess files...

You should be able to find some good information in the Apache Section of the Library, and some more infomation in the Forum Charter.

The following code should give you a good start:

RewriteEngine on
RewriteRule ^somepath/page\.html$ /the/newpath/page.html [R=301,L]

Hope this helps.

Justin

matthewwithanm

10:22 pm on Jan 25, 2006 (gmt 0)

10+ Year Member



Haha.. how did I know that would be the response?

The truth is, I've tried every permutation I can think of. I can get the file to redirect if it has an extension (ie. path/name.abc to path/other/name.html) but for some reason as soon as I take out the extension, it stops working.

jdMorgan

1:15 am on Jan 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



matthewwithanm,

I can get the file to redirect if it has an extension (ie. path/name.abc to path/other/name.html) but for some reason as soon as I take out the extension, it stops working.

Please post an example of your working code and of the non-working code, so that we can discuss it. This way, we can see if it is a simple syntax error, a logical error, or something in your server config that's preventing it from working. Otherwise, you're relying on someone who has an awful lot of spare time to try to guess all the possibilities and write them up for you, and such members are rare.

Jim

jd01

1:16 am on Jan 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Gotta love the standard reply =)

How about we try to remove the end anchor from the rule and see if it will register by using the implicit 'and everything else...'?

RewriteEngine on
RewriteRule ^somepath/page http://www.example.com/the/newpath/page.html [R=301,L]

Justin

BTW:

I've tried every permutation I can think of.

Just copy and paste *something* that doesn't work next time, so we don't feel like we are inventing the wheel --- we won't laugh, I promise --- some of us still forget silly stuff, EG it is always good practice to use a canonical URL if you are redirecting and a relative URL if you are rewriting. Note the difference in the right side of my two examples --- Use #2.

Hey Jim, there's that timing thing again...

matthewwithanm

2:09 am on Jan 26, 2006 (gmt 0)

10+ Year Member



I'll work on it again and post soon. I haven't been saving my mistakes so I don't know what I had before.

EDIT:

You know what? I have no idea what I was doing wrong (other than working too late). The rule's working fine now. Still, could somebody please explain what dropping the trailing $ does?

RewriteRule ^a/(.*)$ /c/a/$1.html [NC]

Thanks.

jdMorgan

2:46 am on Jan 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Still, could somebody please explain what dropping the trailing $ does?

In the case of your rule, it would change nothing, since your final pattern-part is ".*", which will match anything or nothing.

A possible explanation for your didn't-work-before-but-works-now situation is that you had a copy of the original request result cached in your browser. Always flush your browser cache after making any change to access control code or rewrites of any kind; Otherwise, your browser will serve up whatever it has cached for that URL, and it won't send the request to your server. Therefore, you see no change, even with new code on the server.

The only exception to this is when you know that both the old and new content have been tagged as uncacheable by your server-side code, or to have very short expiry times.

Jim

matthewwithanm

4:13 am on Jan 26, 2006 (gmt 0)

10+ Year Member



Thank you so much for your help and explanation.

matthewwithanm

5:27 am on Jan 26, 2006 (gmt 0)

10+ Year Member



Another question-

This time I want to redirect 'domain.com/input' to 'domain.com/input.php'.

I came up with the following, that redirects everything without a dot or slash to specificFile.php, but I don't know how to use parenthesis to capture the input.

RewriteRule!.*\.php$ specificFile.php [NC]

Thanks again.

jdMorgan

2:49 pm on Jan 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can't capture the content of a negative match, so this gets just a bit more complicated:

# If URL does not contain a period or end with a slash
RewriteCond %{REQUEST_URI} !(\.¦/$)
# append .php to requested URL
RewriteRule (.*) /$1.php [L]

Note: Change the broken pipe character "¦" above to a solid pipe before use; Posting on this forum modifies that character.

Jim

extras

2:56 pm on Jan 26, 2006 (gmt 0)

10+ Year Member



We can avoid negative match with a bit of effort, in some cases.

Example of putting the negative match within main Regex:
(No period, Not ending with a slash)
RewriteRule ^([^.]*[^./])$ $1.php [L]

It's probably easier to understand Jim's version, though.

PS.

Maybe this is what matthew wants. (No period, no slash, at all)
RewriteRule ^([^./]+)$ $1.php [L]

matthewwithanm

4:51 pm on Jan 26, 2006 (gmt 0)

10+ Year Member



Thanks again. I think I'm starting to get the hang of this now.