Forum Moderators: phranque
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
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
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...
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.
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
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.
# If URL does not contain a period or end with a slash
RewriteCond %{REQUEST_URI} !(\.¦/$)
# append .php to requested URL
RewriteRule (.*) /$1.php [L]
Jim
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]