I have a few rewrite urls from .htaccess which not working anymore on nginx. both giving me 404
.htaccess urls:
RewriteRule ^password/reset?$ index.php?a=password&b=reset [L]
RewriteRule ^email/verification/(.*)?$ index.php?a=email&b=verification&hash=$1 [L]
nginx rewrite urls
rewrite ^/password/reset?$ index.php?a=password&b=reset break;
rewrite ^/email/verification/(.*)?$ index.php?a=email&b=verification&hash=$1 break;
Related
I have this .htaccess rewrite rules
AddDefaultCharset UTF-8
RewriteEngine On
RewriteRule ^(administrator) - [L]
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
and the nginx version rewrite rules
charset utf-8;
rewrite ^/$ /public/ last;
rewrite /(.*) /public/$1 last;
location ~* ^/(administrator) {
break;
}
The current .htaccess redirect all requests to /public folder except /administrator request.
With the nginx rules the [domain.tld/rss.php not working] [domain.tld/administrator working] [domain.tld working] [File not found.]
My application stracture is
.
..
index.php [require public/index.php]
administrator/index.php
public/index.php
public/rss.php
public/css
public/js
Sepcifiy an index file : index index.php.
By the way you should put rewrite in locations to avoid the test for every request. It's also better for readability/maitainability.
server {
server_name domain.tld;
root /path/to/root;
location ~ /(administrator|public) {
index index.php;
...
}
location / {
rewrite ^/$ /public/ last;
rewrite ^(.*)$ /public/$1 last;
}
}
I have rewrite URL via htaccess now I want to redirect that URL to another URL.
Now I want to redirect http://www.domain.com/tag/example-page URL to http://www.domain.com/tag/example
redirect 301 /tag/example-page http://www.domain.com/tag/example
Please help me regarding this issue.
Below is my rewrite rule, which I am using for short URL
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
Keep this rule as your very first rule in root .htaccess:
RedirectMatch 301 ^/tag/example-page/?$ /tag/example
I've put about 15 mod_rewrite rules in .htaccess to send traffic from some old pages to the homepage. They all look like this:
RewriteRule ^contact_us.php$ index.php [R=301,L]
RewriteRule ^cookie_usage.php$ index.php [R=301,L]
RewriteRule ^articles.php$ index.php [R=301,L]
RewriteRule ^affiliate.php$ index.php [R=301,L]
RewriteRule ^advanced_search.php$ index.php [R=301,L]
All of them properly redirect to index.php except affiliate.php. It returns a 404 Not Found. There are no other rules that have a word remotely close to "affiliate" in .htaccess which means there are no rules hijacking this one. I'm totally stumped. What am I overlooking?
I have this rule now:
RewriteRule ^/mp3/(.*)$ /music/$1 [L,R=301]
RewriteRule ^/activate/(.*) /index.php?p=activate.php&hash=$1 [QSA]
RewriteRule ^(.*)/([0-9]+)/(.*)$ index.php?p=$1&id=$2&page=$2 [QSA]
RewriteRule ^(.*)/([0-9]+)$ index.php?p=$1&id=$2&page=$2 [QSA]
RewriteRule ^(.*)/$ index.php?p=$1 [QSA]
But moved to Nginx, i've created these rules, but now working, i get 404 only:
location / {
rewrite ^/mp3/(.*) /music/$1 permanent;
rewrite ^/activate/(.*) /index.php?p=activate.php&hash=$1 permanent;
rewrite ^(.*)/([0-9]+)/(.*)$ index.php?p=$1&id=$2&page=$2 break;
rewrite ^(.*)/([0-9]+)$ index.php?p=$1&id=$2&page=$2 break;
rewrite ^(.*)/$ index.php?p=$1 break;
}
How to convert QSA rules to Nginx?
Unlike apache's mod_rewrite, you can match against the query string in nginx's rewrite command. So you'd want something like:
location / {
rewrite ^/mp3/(.*) /music/$1 permanent;
rewrite ^/activate/([^\?]*)(?:\?(.*))? /index.php?p=activate.php&hash=$1&$2 permanent;
rewrite ^(.*)/([0-9]+)/([^\?]*)(?:\?(.*))?$ index.php?p=$1&id=$2&page=$2&$3 break;
rewrite ^(.*)/([0-9]+)(?:\?(.*))?$ index.php?p=$1&id=$2&page=$2&$3 break;
rewrite ^(.*)/(?:\?(.*))?$ index.php?p=$1&$2 break;
}
I am using mod_rewrite in .htaccess to redirect from .php to .html like this:
RewriteRule ^([^.]+).html$ /$1.php [QSA,L]
It works in a web browser, but Google is crawling the old URLs i.e. index.php and faq.php.
How I can redirect the index.php to index.html? My website URL is: http://www.21flats.com/.
Add [QSA,L,R=301] so Google will see that there has been a redirect. This adds a 301 redirect to the rewrite for search engines:
RewriteRule ^(.*)\.php$ /$1.html [QSA,R=301,L]
You have the redirect backwards, so change it and add the 301 rule and you are good.
You can try something like this:
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L]