Let's take something like this question. So, I'm looking to rewrite:
http://www.example.com/test to
http://www.example.com/page.php?v=test
with:
RewriteRule ^(.*)$ page.php?v=$1 [L]
This takes me to the page http://www.example.com/test/?v=test
Why does it not stay on http://www.example.com/test without the trailing slash and the query string.
PS: I'm using WampServer on Windows
in RewriteRule don't put / and the begining!
RewriteRule ^(.*)$ page.php?id=$1 [L]
The problem arises if test is an existing directory on the server. Apache then uses the DirectorySlash Directive to "fix up" the URL pointing to the directory by adding a trailing slash.
I'm answering my own question here, but the solution (as the above link states) is to add DirectorySlash Off to .htaccess
Related
I try to create redirect rules. I have two URLs www.example.com/speak and www.example.com/speaking. I added below lines to my .htaccess
RewriteRule speaking$ /?utm_source=offline&utm_medium=books&utm_campaign=bookmarks_uk [NC,QSA,R=301]
RewriteRule speak$ /?utm_source=offline&utm_medium=bookmarks&utm_campaign=english_course [NC,QSA,R=301,L]
but each time I'm redirected to /?utm_source=offline&utm_medium=books&utm_campaign=bookmarks_uk.
I swapped them around but it didn't work.
Any idea?
Those rules should work fine, but can be improved to cater for the trailing slash. Also use ^ to mark the beginning of the string and $ to mark the end of the string.
RewriteRule ^speaking/?$ /?utm_source=offline&utm_medium=books&utm_campaign=bookmarks_uk [NC,QSA,R=301]
RewriteRule ^speak/?$ /?utm_source=offline&utm_medium=bookmarks&utm_campaign=english_course [NC,QSA,R=301,L]
I suspect you problem is browser caching. Try clearing your browser cache and restarting your browser.
You are being redirected because you are using the R=301 flag. If you are trying to access /speaking and show the contents of /?utm_source=offline&utm_medium=books&utm_campaign=bookmarks_uk, then you need to remove the flag in question.
Your resulting rules should look like this:
RewriteRule ^speaking/?$ /?utm_source=offline&utm_medium=books&utm_campaign=bookmarks_uk [NC,QSA,L]
RewriteRule ^speak/?$ /?utm_source=offline&utm_medium=bookmarks&utm_campaign=english_course [NC,QSA,L]
The following rule works to remove the trailing slash for all pages in my web root:
RewriteRule ^(.+)/$ /$1 [L,R=301]
However, it does not work when placed in a sub folder. I've also tried:
RewriteRule (.*)/$ /$1 [L,R=301]
to no avail. What happens is, it redirects
http://example.com/testfolder/testpage/
to
http://example.com/testpage
What am I missing? Thanks in advance!
Have you tried omitting the leading slash from your replacement?
RewriteRule ^(.+)/$ $1 [L,R=301]
It's just a stone's throw from what you have. The context of the directory rewrite (being "in" /testfolder) may be the root cause of the trouble.
you may be able to define this in variables also make sure Rewrite mods are enabled in your php.ini if there is no other way of doing what you need.
The trailing slash fixup is being done by mod_dir. Rewrites in your per-directory context are re-injected into the URL processing chain and subject to the fixup again.
The behavior is configurable. E.g.
<Directory /path/to/wherever>
DirectorySlash Off
...
</Directory>
The context for this is not only Directory: it is server config, virtual host, directory, .htaccess.
I'm using mod_rewrite to rewrite /products to /products.php. I've got this code in /.htaccess
Options FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-z]+)$ /$1.php [PT,L]
Unfortunately there is also a folder /products/ on my server.
My problem is, when I try to access http://mydomain.com/products my request is redirected to http://mydomain.com/products/ and showing me an error because I don't have an index for that directory.
Who is redirecting me? Apache, my UserAgent?
How do I prevent that this happens without changing the folder name or the rewrite rule?
You need to look up the "DirectorySlash Directive".
The DirectorySlash directive
determines whether mod_dir should
fixup URLs pointing to a directory or
not.
http://httpd.apache.org/docs/2.2/mod/mod_dir.html
You could also try adding an optional slash to you rewrite rule:
RewriteRule ^([a-zA-z]+)/?$ /$1.php [PT,L]
Trailing slashes problem
You have probably enabled MultiViews on your Apache.
Every browser is adding the trailing slash after your request if it doesn't by ".something" because it thinks it is a folder. To avoid this, your rewrite rule should look like this:
RewriteRule /products(.*)$ /index.php?page=products
OR
RewriteRule /products /index.php?page=products
That way, it will rewrite every request with "/products" in it, with or without the trailing slash.
The only thing is your folder /products/ will not be accessible by an http request. If you want so, you must change the name of the folder or the page name.
I want to 301 redirect an entire website, but exclude everything in a folder called /uploads which exists in the /root directory.
I have googled for this, but didn't come up with anything, or I didn't think what I saw was right.
Can we crack this?
Try this mod_rewrite rule:
RewriteEngine on
RewriteRule !^uploads($|/) http://example.com%{REQUEST_URI} [L,R=301]
This rule does match any URL path that does not begin with either /uploads or /uploads/ (leading / is missing in the pattern due to the path prefix removal when used in .htaccess files) and redirects the request to the corresponding path at example.com.
Simple answer I just stumbled upon myself.
At the top before any other calls add the following
RewriteRule ^(uploads) - [L]
I think you want this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/uploads/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
If you get 500 Internal Error then double-check that you have a space between } and ! on the second line.
A mod-alias based solution
Redirect all except a specific folder
Add the following line to your root/.htaccess :
RedirectMatch 301 ^/((?!uploads).*)$ http://newdomain.com/$1
This will redirect all pages (excluding /uploads/*) from your old domain to the newdomain.
I've just installed WAMP, and now I'm trying to copy my files from my server to my local machine. I have a rewrite rule like
RewriteRule .* /index.php?url=$0 [L,QSA]
But that seems to redirect to http://localhost/index.php when I actually want http://localhost/mysite/index.php. So I figured
RewriteBase /mysite
Would do the trick, but that doesn't appear to be the case. Doesn't seem to work that way from my readings. Is there any way that I can fix this without changing my rewrite rules? I basically just want the base of my site to be in some subfolder.
I got this working by having a trailing slash on the RewriteBase string:
RewriteBase /aliasdirectory/
if you’re using relative paths in your substitution, you don’t need to alter the base URI path:
RewriteRule .* index.php?url=$0 [L,QSA]