I have searched through the related mod_rewrite qustions but I can't anything specific enough so I'll post:
Heres my rule that adds a trailing slash:
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
Only I want to exclude one specific directory eg. /mydirectoryname/ and not add the trailing slash to anything that starts with that. Reason being its breaking some of my ajax calls.
Add another RewriteCond:
RewriteCond %{REQUEST_URI} !/mydicrectoryname)/
You can descibe that with just one condition:
RewriteCond $0 !^mydirectoryname(/|$)
RewriteRule ^[^\.]+[^/]$ /$0/ [R=301,L]
Related
This are my urls
http://test.aDomain.com/x-61bff
http://test.aDomain.com/x-ssmJhs54as65d4
http://test.aDomain.com/x-115545454
my rewrite rules
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
RewriteRule ^create-account create-account.php [L]
RewriteRule ^account account.php [L]
RewriteRule ^impress impress.php [L]
RewriteRule ^(x\-[a-zA-Z0-9]+) redirect.php?code=$1 [L]
The result is
http://test.aDomain.com/redirect.php
But why? it should be
http://test.aDomain.com/redirect.php?code=x-61bff
i dont get it ... can anybody help?
mainly because you got the regex wrong:
the / between the host-name and the path is really part of the path (e.g. it is /x-61bff instead of x-61bff). however, your regex matches only an x at the very beginning, thus /x-61bff will never match.
the minus-sign has only a special meaning within square brackets (e.g. [A-Z]); outside it is just another character, there is no need to escape it
so your rewrite-rule really should look like:
RewriteRule ^/(x-[a-zA-Z0-9]*) redirect.php?code=$1 [L]
I have static files in several directories on my website.
I was trying to create a rule that will instruct the rewrite engine to disregard all of those static files.
something like this:
#RewriteCond %{REQUEST_URI} !\.*(jpg|css|js|gif|png|swf)$ [NC]
this seems to work only in some of the cases, but not all of them.
As a solution I decided to match the entire request URI of any requested file with one of the following extensions, and create an 'empty' rewrite rule for them, that will redirect the user to the precise request URL, basically.
To achieve that I did the following:
RewriteCond $1 \*.(js|ico|gif|jpg|png|css|swf|mp3|wav|txt)$ [NC]
RewriteRule ^(.*) \$1 [R=301,L]
This won't work for some reason, no useful information at the access.log and error.log either.
Thanks in advance for any assistance!
Full code:
#Catch static content and don't modify it
RewriteCond $1 \*.(js|ico|gif|jpg|png|css|swf|mp3|wav|txt)$ [NC]
RewriteRule ^(.*) \$1 [R=301,L]
#prevent looping
RewriteCond %{QUERY_STRING} !clientId=
#redirect sub domain to the client page cutl.mysite.com = mysite.com/in/index/php?clientId=curl
RewriteRule ^(.*)$ /in/index.php?uclinetId=$1 [L]
Rather than maintaining a long list of file extensions for exclusion you can simply use:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
Which means apply next RewriteRule if request is NOT for a real file or a symbolic link.
Probably your backslash should be between the asterix and the dot?
RewriteCond $1 *\.(js|ico|gif|jpg|png|css|swf|mp3|wav|txt)$ [NC]
RewriteRule ^(.*) \$1 [R=301,L]
I have a RewriteRule on my Apache to make URLs friendly
RewriteRule ^log/(.+)$ script.php?u=$1 [QSA]
This makes http://example.com/log/username get internally redirected to http://example.com/script.php?u=username
This works fine, as long as the username does not contain a trailing period. However, there are usernames, where people chose names like "firstname-L." (note the trailing period)
In this case http://example.com/firstname-L. gets translated to http://example.com/script.php?u=firstname-L (the trailing period is gone)
How can I get this to work?
You can try to check these configuration directives:
RewriteCond %{REQUEST_URI} !^/log/(.+)\.$
RewriteRule ^log/(.+)$ /script.php?u=$1 [QSA]
RewriteCond %{REQUEST_URI} ^/log/(.+)\.$
RewriteRule ^log/(.+)$ /script.php?u=$1. [QSA]
OR:
RewriteCond %{REQUEST_URI} ^/log/(.+)([a-zA-Z0-9_-]{1})$
RewriteRule ^log/(.+)$ /script.php?u=$1 [QSA]
RewriteCond %{REQUEST_URI} ^/log/(.+)\.$
RewriteRule ^log/(.+)$ /script.php?u=$1. [QSA]
I hope, I'm not mistaken.. Now, as the variable of usernames with one character or one character with trailing period won't work in the second code, we can try to replace the second code with this one:
RewriteCond %{REQUEST_URI} ^/log/(.*)([a-zA-Z0-9_-]{1})$
RewriteRule ^log/(.+)$ /script.php?u=$1 [QSA]
RewriteCond %{REQUEST_URI} ^/log/(.*)\.$
RewriteRule ^log/(.*)$ /script.php?u=$1. [QSA]
Or you could try these three liner directives if you doesn't like the four liner:
RewriteCond %{REQUEST_URI} ^/log/(.*)\.$
RewriteRule ^log/(.*)$ /script.php?u=$1. [QSA]
RewriteRule ^log/(.*)([a-zA-Z0-9_-]{1})$ /script.php?u=$1$2 [QSA]
I hope, all these codes above will work!
Your rule appears to be right, perhaps the client or server is stripping the last dot as spurious (checked on YT with Chrome, if you add a dot at the end of a video url (?=xxxxxxxxx to ?=xxxxxxxxx.) and press enter, it gets removed -- actually triggering a 303 HTTP response).
In general, you should use only upper/lowercase letters and non-trailing dashes or dots in urls to compose the so-called slug, which is guaranteed to be handled correctly across all decent browsers and web servers.
I have got this working:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteRule ^(.*) http://domain.co.uk/%1 [L]
So it will redirect test.domain.co.uk to test.domain.co.uk/test
However, I would like it so that it changes the document root rather than the user seeing the redirect. I have read a few articles on here and tried a few things. But I am not sure how to debug any errors, it just doesn't work. The one other that I have tried:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/-
RewriteCond %{HTTP_HOST} ^([^\./]+)
RewriteCond %{DOCUMENT_ROOT}/-%1 -d
RewriteRule ^(.*)$ -%1/$1 [L]
But this doesn't seem to work at all.
Any help would be appreciated.
Ian
Try something like this in your document root:
RewriteEngine On
# check if subdomain folder has the existing file, if so, serve it
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -f
RewriteRule ^(.*)$ /%1/$1 [L]
# check if subdomain folder has the existing directory WITH A TRAILING SLASH, if so serve it
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
RewriteRule ^(.*?)/$ /%1/$1/ [L]
# check if subdomain filder has the existing directory but missing trailing slash, redirect with it
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /$1/ [R=301,L]
This method does 2 things for you, if it's a 404, it won't try to internally rewrite, so if you go to: http://sub.domain.co.uk/this/path/does/not/exist/blah, you won't get a 404 message saying: /sub/this/path/does/not/exist/blah doesn't exist, just /this/path/does/not/exist/blah because the URI wasn't rewritten, and thus not exposing your underlying directory structure.
The second thing is that you probably have DirectorySlash turned on. Which means if you access a directory like: http://sub.domain.co.uk/this/path, missing the trailing slash, mod_dir will want to redirect and add that slash, but it'll probably do it wrong because the URI has been rewritten and will redirect you to: http://sub.domain.co.uk/sub/this/path/. We you pre-emptively add the trailing slash with the correct URI hiding the sub.
RewriteEngine On
RewriteRule ^/ai?$ /Market/publish.jsp [QSA,L,PT]
RewriteRule ^/ar?$ /Market/MailDispatch [QSA,L,PT]
RewriteCond %{HTTP_HOST} !^web\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://web.example.com/$1 [L,R]
#How skip www\. to web\. for this 1 ?
#RewriteRule ^/vi/?([0-9]+)\.htm$ /Market/vi.do?id=$1 [PT,L]
RewriteRule ^/li /Market/list.do [QSA,PT,L]
RewriteRule ^/vi/locations.jsp /Market/locations.jsp [PT,L]
ErrorDocument 404 /notfound.html
Nearly undoable(?) I try http://example.com/vi/{N}.htm should redirect to http://web.example.com/vi/{N}.htm where N is dynamic ID.
Seen mod_rewrite with subdomain and url pattern
There is no clear way to make eg http://example.com/vi/1096.htm pass up to next version http://web.example.com/vi/1096.htm where number is dynamic. I tried
A rule with the following scheme should do it:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^/vi/\d+\.htm$ http://web.example.com%{REQUEST_URI} [L,R=301]
It’s important to put this rule in front of those rules that do an internal redirect. Otherwise an already internally rewritten URL could be rewritten externally.
If you want to use this rule in a .htaccess file, remove the leading slash from the pattern in RewriteRule.