I am having trouble with some rewrite rules on an updated site...I am trying to redirect requests for old directories to new pages, like this:
http://www.mysite.com/olddirectory --> http://www.mysite.com/this-is-the-new-page
the following rule works, but not with a trailing slash on the directory:
ie: /olddirectory redirects correctly, but /olddirectory/ doesn't
RewriteRule ^olddirectory$ this-is-the-new-page [R=301,NC,L]
Any ideas on how to get it to recognise the trailing slash on the dir?
The following rule will do redirect with or without trailing slash:
RewriteRule ^olddirectory/?$ /this-is-the-new-page [NC,R=301,L]
The key is to make trailing slash optional, and that's what ? does: /?
Related
I have a number of URLs which may be entered with or without a trailing slash. They're used as vanity URLs.
.htaccess looks like this:
Redirect 301 /folder/vanity1 http://example.com/differentfolder/
Redirect 301 /folder/vanity2 http://example.com/a/third/folder/
Redirect 301 /vanity3 http://example.com/fourth/folder/
Users type in:
http://example.com/folder/vanity1
http://example.com/folder/vanity2
http://example.com/vanity3
http://example.com/folder/vanity1/
http://example.com/folder/vanity2/
http://example.com/vanity3/
When users type these in with no trailing slash, the redirects are working correctly, ending up at http://example.com/differentfolder/ with one trailing slash as desired.
The problem:
When users type these in with a trailing slash, such as http:/example.com/folder/vanity1/ they redirect to a URL with two trailing slashes, such as http://example.com/differentfolder// which throws a 404 error.
I have tried:
Removing all other lines from .htaccess, except these three vanity URLs and the standard WordPress block required to keep the site functioning. The WordPress block appears at the very end of the file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I checked not only with my browser but with WebConfs HTTP Header Check to make sure I wasn't seeing a cached rule. Still the same, if you type a vanity URL with no trailing slash it works; if you type it with one trailing slash, it redirects to two trailing slashes which causes a 404.
Adding a trailing slash in my rules:
Redirect 301 /folder/vanity1/ http://example.com/differentfolder/
This works fine if the user types the trailing slash, but if they leave off the slash it does not redirect and 404s because the URL http://example.com/folder/vanity1/ does not actually exist.
Question:
Is there a different way to 301 redirect the vanity URLs to a final destination with only one trailing slash, no matter which way the visitor types in the vanity URL - with or without a trailing slash?
This works as documented in Redirect,
Additional path information beyond the matched URL-path will be appended to the target URL.
This means /folder/vanity1 works only as a prefix, and an additional slash or anything else in the requested URL will be appended to the target, e.g. http://example.com/folder/vanity1/hi/there would result in http://example.com/differentfolder//hi/there
If you want to match exactly these two URLs, /folder/vanity1 and /folder/vanity1/, you must use either RedirectMatch or mod_rewrite
With a RewriteRule this would look like
RewriteRule ^folder/vanity1/?$ /differentfolder/ [R,L]
This pattern matches both with or without a trailing slash because of /?, which denotes an optional slash. The other rules would look similar.
When everything works as it should, you may replace R with R=301 (permanent redirect). Never test with R=301.
And with RedirectMatch, it would look almost the same
RedirectMatch ^/folder/vanity1/?$ /differentfolder/
Same disclaimer applies here, when it works as expected, you may set the status code to 301.
What I need is any subfolder to be passed as a parameter to the root index.php
This is the code and it actually works.
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^(.+?)(/[^/]*|)$ index.php?dir=$1/ [L,QSA]
There is a problem:
When the url is like this (no end slash after 'projects'):
http://example.com/projects
the rewrite rule changes the link in the address bar and it looks like this:
http://example.com/projects/?dir=projects/
Is there a chance the url in the address bar always stays the same(no matter if there is an end slash or not) so the dir parameter is not visible to the user?
I tried with multiple rules - the first one to add an end slash, and then the second rule to pass the directory as parameter, but with no luck so far.
EDIT: so far thanks to w3d I managed to get it working. In the .htaccess just add:
DirectorySlash Off
tl;dr Make the trailing slash mandatory in the RewriteRule pattern (and remove the DirectorySlash Off directive, ie. keep it On).
RewriteRule ^(.+)/$ index.php?dir=$1/ [L,QSA]
As suggested in comments, this "strange" redirect is the result of mod_dir's DirectorySlash On directive, which is On by default. This can be quickly resolved by including DirectorySlash Off at the top of your .htaccess file (or making the trailing slash mandatory - see above and below).
The DirectorySlash On directive instructs Apache to automatically append a slash to URLs that end in a file system directory. In this sense it is "fixing" the URL. mod_dir achieves this with a 301 external redirect.
So, what is actually happening in the above, when DirectorySlash is enabled, is:
Initial request:
/projects (no trailing slash)
Internal rewrite in .htaccess:
/index.php?dir=projects/ (note that the request URL is still /projects)
mod_dir now kicks in and "fixes" the initial request (/projects --> /projects/) by appending a slash to the end of the URL-path. However, the query string from the rewritten URL (above) is passed through:
/projects/?dir=projects/ (this is a 301 external redirect, ie. a new request!)
Internal rewrite in .htaccess (again - new request):
/index.php?dir=projects/&dir=projects/ (note that the request is still /projects/?dir=projects/)
The doubling of the dir=projects/ query param is a result of the QSA flag on the RewriteRule (which I assume is required for other requests?). Your PHP script simply sees a single dir GET param (the later overwrites the former), unless you included dir[]=$1/ in your RewriteRule and you will end up with a 2-element array!
Your RewriteRule pattern also looks unnecessarily complex. You could simply make the trailing slash optional. ie:
RewriteRule ^(.+?)/?$ index.php?dir=$1/ [L,QSA]
Alternatively, having said all the above, you should probably leave DirectorySlash On (default) and simply make the trailing slash mandatory! For example:
RewriteRule ^(.+)/$ index.php?dir=$1/ [L,QSA]
mod_dir will now kick in before your internal rewrite (since it won't match without a trailing slash). This is also better for canonicalising your URLs and there are also potential security risks with turning it off.
Reference:
http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryslash
I want to do a particular redirect
/mytag
redirect to file.php
the thing is it can be either /mytag/ or /mytag only without the final slash.
can't find this.. thanks
tried with this:
RewriteEngine on
options +FollowSymLinks
RewriteRule ^mytag(.*)$ file.php
As long as you don't have a file or directory called "mytag", then you can just end the regex with /? to make the ending slash optional:
RewriteRule ^mytag/?$ file.php [L]
Though what you already have should allow both the slash or no slash at the end. This won't work if you have a directory called mytag because mod_dir and DirectorySlash will always redirect a request for a directory that doesn't have a trailing slash to the same request with the trailing slash. If you have Multiviews turned on, and you happen to have a mytag.php (or something similar), mod_negotiation will kick in and the rewrite rule will get ignored.
At the site were some directories with html files like:
site.com/folderone/index.html
site.com/foldertwo/index.html
and it was available by links like:
site.com/folderone
site.com/foldertwo
now all these directories moved to /old-pages:
site.com/old-pages/folderone/index.html
site.com/old-pages/foldertwo/index.html
but old links should be available, so .htaccess file:
RewriteEngine on
RewriteRule ^folde(.*)$ /old-pages/folde$1 [L]
it's rewrite correct site.com/folderone/ and site.com/folderone/index.html
the problem is: for site.com/folderone it's not rewrite but redirect to site.com/old-pages/folderone/
The redirect is probably happening because of mod_dir and the DirectorySlash directive, which redirects requests for a directory that is missing the trailing slash so that the trailing slash is there. There's a good reason why that happens, as there's a info disclosure security concern without the trailing slash.
What you can do to avoid the redirect is either turn it off (not recommended):
DirectorySlash Off
or include the slash via mod_rewrite so that the two modules won't interfere with each other over the same request:
RewriteRule ^folder([^/]+)$ /folder$1/ [L,R=301]
You must add that before your other rewrite rules so that it gets applied first. Then your other rule should work.
RewriteEngine on
RewriteRule ^$ http://my-site.com/directory [R=301,L]
This redirects my root page to
http://my-site.com/directory/
(notice the trailing slash).
How can I make .htaccess omit the trailing slash when generating the URL?
This is because directory is an existing directory, this is not a rewritten url.
Use another word instead of :
RewriteEngine on
RewriteRule ^$ /mypath [R=301,L]
RewriteRule ^mypath$ /directory/ [L]
Quote from noupe.com :
The filesystem on your server will always take precedence over the
rewritten URL. For example, if you have a directory named “services”
and within that directory is a file called “design.html”, you can’t
have the URL redirect to “http://domain.com/services”. What happens is
that Apache goes into the “services” directory and doesn’t see the
rewrite instructions.
To fix this, simply rename your directory (adding an underscore to the
beginning or end is a simple way to do that).
Bonus : To remove trailing slash in every urls :
RewriteEngine On
RewriteRule ^(.*)/$ $1 [R,301,L]