I'm trying to set up some path rewrites on two separate servers, one using mod-rewrite on Apache and one using HttpRewriteModule on Nginx. I don't think I'm trying to do anything too complex, but my regex skills are a little lacking and I could really use some help.
Specifically, I'm trying to transform a formatted URL into a query string, so that a link formatted like this:
http://www.server.com/location/
would point to this:
http://www.server.com/subdirectory/index.php?content=location
Anything extra at the end of the formatted URL should be appended to the "content" parameter in the query string, so this:
http://www.server.com/location/x/y/z
should point to this:
http://www.server.com/subdirectory/index.php?content=location/x/y/z
I'm pretty sure this should be possible using both Apache mod-rewrite and Nginx HttpRewriteModule based on the research I've done, but I can't see to get it working. If anyone could give me some pointers on how to put together the expressions for either or both of these setups, I'd greatly appreciate it. Thanks!
In nginx you match "/location" in a rewrite directive, capture the tailing string in the variable $1 and append it to the replacement string.
server {
...
rewrite ^/location(.*)$ /subdirectory/index.php?content=location$1 break;
...
}
In Apache's httpd.conf this looks quite similar:
RewriteEngine On
RewriteRule ^/location(.*)$ /subdirectory/index.php?content=location$1 [L]
Have a look at the examples at the end of this page: https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
Search string: (.+)/location/(.*)$
replacement string: $1/subdirectory/index.php?content=location/$2
For Apache, in the htaccess file in your document root, add:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/subdirectory/index\.php$
RewriteRule ^(.*)$ /subdirectory/index.php?content=$1 [L]
In nginx, you want to first make sure requests for /subdirectory/index.php get passed through, then rewrite everything else:
location ~ /subdirectory/index\.php$
{
}
location /
{
rewrite ^(.*)$ /subdirectory/index.php?content=$1 break;
}
This would probably be the best way to do it in nginx:
location ^~ /location/ {
rewrite ^/(location/.*)$ /subdirectory/index.php?content=$1 last;
}
For more details, see:
http://nginx.org/r/location
http://nginx.org/r/rewrite
Related
I've some dynamic urls. If I took a url with a query parameter, it leads to 404 page. So I would like to do a redirection using htaccess. I tried many possible solutions, and none of them worked.
Url structure will be /jobs/job-***.html?something and which I need to redirect to /jobs/job-***.html
I tried something like this, but returned 500 error;
RewriteRule ^jobs/job-([0-9]+).html?$ jobs/job-$1 [NC, L]
Please help to solve this problem.
You may use this rule:
RewriteCond %{QUERY_STRING} .
RewriteRule ^jobs/job-(\d+\.html?)$ %{REQUEST_URI}? [NC,L,R=301]
? after $1 in target will remove any query string.
You may use the following regex:
^\/jobs\/job-(\d+)\.html\?\S*$
(This essentially captures the job number)
and then replace it with:
/jobs-$1.html
Demo
I am using Apache 2.4.7. I use mod_rewrite to alter some urls.
I want to rewrite http://example.com/servicename/oldpage?id=abcto http://example.com/servicename/newpage.
Other similar rewrites work so I belive the ? inside url is causing problems.
I have tried escaping it with \.
This works as there is no ? in url:
RewriteRule ^/servicename/old /servicename/new
But these don't work:
RewriteRule ^/servicename/oldpage?id=abc /servicename/newpage
RewriteRule ^/servicename/oldpage\?id=abc /servicename/newpage
I have also tried using RewriteCond from examples like this: .htaccess rewrite URL with a question mark "?" but I didn't manage to get them work.
How should rewrite url that contains question mark?
EDIT: I tried solutions given in Match Question Mark in mod_rewrite rule regex but was not able to make them work for me. That question is about preserving query string when rewrite while I want to remove it when rewriting.
RewriteRule pattern is matched against the part of the URL after the hostname and port, and before the query string.
When the requested URI contains a query string, and the target URI does not, the default behavior of RewriteRule is to copy that query string to the target URI. Using the [QSD] flag causes the query string to be discarded.
So, this should work:
RewriteRule ^/servicename/oldpage /servicename/newpage [QSD]
I know there are plenty of questions how to replace part of URL with something else (or nothing) using .htaccess but I really suck both in regular expressions and in .htaccess.
How can I to convert URL like /v0/A8B9DEBF512F929144257AEE00262C16/$File/IMG_8819.jpg to /v0/A8B9DEBF512F929144257AEE00262C16/IMG_8819.jpg? (Without $File/, instead of A8B9DEBF512F929144257AEE00262C16 and IMG_8819.jpg there may be any combination of digits and letters).
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^(v0/[^/]+)/\$File/(.*)$ /$1/$2 [L,NC,R=301]
I have this case in htaccess
RewriteRule ^.*$ https://example.com/ [CO=ref:%{HTTP_REFERER}:example.com:0:/]
All I need is to escape string %{HTTP_REFERER} but after half an hour googling and reading apache docs it seems I can't solve this one :)
So, how do I escape strings in apache?
If you want escaping behavior you will need to add this line in your Apache config file:
RewriteMap escape int:escape
Then restart the Apache server.
Further you need to modify your rewrite rule like this:
RewriteRule ^ https://example.com/ [CO=ref:${escape:%{HTTP_REFERER}}:example.com:0:/]
I'm working on a webpage that takes a URL as a parameter, and would like it to be easily indexed by search engines. One requirement is that each URL appears as a directory.
My script is in the format:
myscript?url=<a url>&page=1
I'd like redirects to look something like:
lookup/<a url>/page:1/
The URL is predictably giving me trouble... I just want to tell mod_rewrite to select anything after "lookup/" and before "/page:". Of course, nothing is ever as simple as it could be.
Here's the rewrite as it is now:
RewriteEngine on
RewriteRule ^/lookup/(.+)/page:([0-9]+)(/?)$ /myscript?url=$1&page=$2 [L]
This works great, except it fails when URLs are properly encoded. Take the example of "www.google.com/finance". Here's what happens when I enter these URLs into my browser's address bar:
#this works
lookup/www.google.com/finance/page:1/
#this doesn't work. url is cut off before the ?
lookup/www.google.com/finance?foo=bar/page:1/
#doesn't match rewrite at all!
lookup/www.google.com%2Ffinance/page:1/
I'm at a loss as to how to do this... Shouldn't (.+) select anything? Do I need to tell mod_rewrite to ignore query parameters somehow?
Try this:
RewriteCond %{THE_REQUEST} ^GET\ /lookup/([^\s]+)/page:([0-9]+)/[?\s]
RewriteRule ^/lookup/ /myscript?url=%1&page=%2 [L]
But you should really consider encoding that embedded URL properly instead of just guessing where it might end. So /lookup/www.google.com/finance?foo=bar/page:1/ should be at least /lookup/www.google.com/finance%3Ffoo=bar/page:1/ so the ? is part of the URI path and not the indicator for the query.