mod_rewrite with trailing period in URL - mod-rewrite

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.

Related

Relative path and mod_rewrite issue

Have a trouble with mod_rewrite.
I want :
htp://example.com/a/some_text -> htp://example.com/?p1=some_text and
htp://example.com/b/some_text -> htp://example.com/?p1=some_text
So, I type:
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^(.*)$ ?fsearch=$2 [QSA]
and get wrong relative paths in css, such as htp://example.com/a/CSS/main.css instead of htp://example.com/CSS/main.css. And get nothing in $2 too.
Help, please
$2 tries to pick the second capture group of the RewriteRules subject, but there is only one! You probably mean %2 which picks from the RewriteCond...
Since you only append a query string in your RewriteRule the original URI (path) is left untouched.
You can simplify the pattern in the RewriteRule since you are not interested in the subject anyway.
This is probably what you are looking for:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^ /?fsearch=%3 [QSA]
Much better however would be to rewrite directly to whatever script you actually want to process the request to safe another rewriting round to find the index document, so something like:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^ /index.php?fsearch=%3 [QSA]
And unless you decide to use the first capture group in the RewriteCond you can also simplify that further:
RewriteEngine on
RewriteCond %{REQUEST_URI} \/(a|b)\/(.*)$
RewriteRule ^ /index.php?fsearch=%2 [QSA]

RewriteCond Simple Issue

I need the URl http://mydomain.com/careers to go to http://mydomain.com/#!/careers
For what it's worth I've tried numerours variations around this with no success
RewriteCond %{REQUEST_URI} !^/careers$
RewriteRule (.*) /#!/careers [QSA,L]
Can anyone help?
"!" in your RewriteCond line means "not"...
Also, "[QSA,L]" means:
L means this is last rule (processing terminates after matching this one) and
QSA means query string append
But, becase R flag was not specified, this is done by sub-request and not a redirect, so the actual URL in your browser does not change...
Try this:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/careers$
RewriteRule (.*) /#!/careers [R,L,QSA]
Hope this helps

Replace ? in request with RewriteRule

site.com/link?p=2
give $_GET['p']==2 even though i've already made
site.com/link
rewrite to
site.com/index.php?page=link
So, i'm trying to replace site.com/link?p=2
with site.com/link&p=2
RewriteEngine on
RewriteRule (.*)\?(.*) $1\&$2
RewriteCond %{REQUEST_URI} !\....$
RewriteRule ^(.*)$ /index.php?p=$1
It doesn't work!
RewriteRule cannot see query strings (the ? and anything after it) on the left-hand side; it matches only on the path part of the URL.
But the good news is, all you probably need to do is this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !\....$
RewriteRule ^(.*)$ /index.php?p=$1 [QSA]
The QSA option, Query String Add, tells your RewriteRule to add to the query string instead of replacing it (the default behavior, which doubtless prompted the whole issue).

mod_rewrite - redirect from one domain to another and preserve trailing values in url

I think this is a pretty straight forward question in mod_rewrite:
I got one domain, which needs to redirect to another, but keep any value after last slash (/) in the first URL, over to the second.
domain.com/4433 should transfer to domain.com/folder/?p=4333
Listed for clarity:
From: domain.com/4433
To: domain.com/folder/?p=4333
Any ideas?
Edit:
Did some testing, we found the following solution:
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^([0-9a-z]*)$ /folder/?p=$1 [NC]
sincerely,
- bakkelun
In case you don't really want to redirect but to have pretty URLs, you can use
RewriteEngine On
RewriteRule ^/(.+)$ /folder?p=$1 [L]
This takes everything after the first slash and inserts it at the $1 - but only if there's something after the slash. It doesn't issue a redirect so the users won't notice.
Without any further information, try this:
RewriteEngine on
RewriteRule ^/([^/]+)$ /folder/?p=$1
If you want to use the rule in a .htaccess file, remove the leading slashes.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ domain.com/folder?p=$1 [R=301,L]
Just in case: domain.com = domain1.com and domain2.com? domain1.com should be redirected to domain2.com? Both run on the same server (optional)?
[EDIT:]
If you really only want to do the thing as stated in the comment, then do the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^4433$ http://domain2.com/folder/?p=4433 [R=301,L]
Else, as Benedikt Eger said, or with R=301 if you want real redirection.
Or, if you want it to redirect only on numbers, then do the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^([0-9])+$ http://domain2.com/folder/?p=$1 [R=301,L]
RewriteCond checks, if defined vhost is domain1.com, but not domain2.com, then the rewrite rule is applied, and redirects via HTTP status 301 [R=301] only number strings (0-9)+ consisting of at least one number to the specified URL. [L] makes this the last rule applied.

Simple mod rewrite question

Here is my current .htaccess file:
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
As you can see, requests to http://domain.com go to http://domain.com/index.html. I want to change this so that they go to http://domain.com/foo, please note that does not exist as a file or folder, it is handled by rails. How do I do this? Note that I have tried the following and it doesn't work:
RewriteRule ^$ foo [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
Thanks!
You're probably safe to just change the first line to this:
RewriteRule ^$ foo [QSA,L]
The L flag tells mod-rewrite that it shouldn't apply any other rules after that one. The problem right now is that the second rule gets applied after the first one, and you end up at "foo.html", instead of "foo", right?
The difference between you trying to send to "foo" and the original redirect to "index.html" is that the second rule applies to requests that do not include a period. So when the first rule was redirecting to "index.html", after it was used, the second rule was no longer valid. However, now that you're not redirecting to a location with a period in it, the second rule gets applied after the first one, so you get a double-redirect.
In addition, you may be able to drop the QSA flag from the first line, it depends on your site though. If someone accesses the site like http://domain.com/?user=fred, do you want to send them to http://domain.com/foo?user=fred, or just http://domain.com/foo? If you don't need the Query String Appended, you can drop the QSA flag, and just have:
RewriteRule ^$ foo [L]

Resources