I used the following rule Remove Trailing slashes from all URL
#remove /
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
the rule is working fine expect for home page /
its through
ERR_TOO_MANY_REDIRECTS
i've enabled mod_ rewrite logs to trace the rule
10.64.159.12 - - [14/Feb/2018:12:04:16 +0400] [www.mywebpage.com/sid#7effdd821860][rid#7eff00068100/initial] (2) init rewrite engine with requested uri /
10.64.159.12 - - [14/Feb/2018:12:04:16 +0400] [www.mywebpage.com/sid#7effdd821860][rid#7eff00068100/initial] (3) applying pattern '^(.*)$' to uri '/'
10.64.159.12 - - [14/Feb/2018:12:04:16 +0400] [www.mywebpage.com/sid#7effdd821860][rid#7eff00068100/initial] (3) applying pattern '^(.*)\\/(\\?.*)?$' to uri '/'
10.64.159.12 - - [14/Feb/2018:12:04:16 +0400] [www.mywebpage.com/sid#7effdd821860][rid#7eff00068100/initial] (2) rewrite '/' -> ''
10.64.159.12 - - [14/Feb/2018:12:04:16 +0400] [www.mywebpage.com/sid#7effdd821860][rid#7eff00068100/initial] (2) explicitly forcing redirect with http://www.mywebpage.com/
10.64.159.12 - - [14/Feb/2018:12:04:16 +0400] [www.mywebpage.com/sid#7effdd821860][rid#7eff00068100/initial] (1) escaping http://www.mywebpage.com/ for redirect
10.64.159.12 - - [14/Feb/2018:12:04:16 +0400] [www.mywebpage.com/sid#7effdd821860][rid#7eff00068100/initial] (1) redirect to http://www.mywebpage.com/ [REDIRECT/301]
any idea why its not working for home page url and works for all other pages url
To remove trailing slash keep this rule your first rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=301,L]
Related
I have a PHP application that uses GET variables to determine language, that is:
http://example.com/?lang=en
And I'm writing a mod_rewrite rule so that people can access it using SEO friendly URLs:
http://example.com/en/
My .htaccess looks like this so far:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[a-z]{2} /test.php?lang=$0 [L]
(The test.php thing is just a test, not the name of my real app).
But when I access:
http://example.com/fr/blabla
What I get in my rewrite_log is the following:
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b8ce58/initial] (3) [perdir /var/www/html/example/] add path info postfix: /var/www/html/example/fr -> /var/www/html/example/fr/blabla
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b8ce58/initial] (3) [perdir /var/www/html/example/] strip per-dir prefix: /var/www/html/example/fr/blabla -> fr/blabla
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b8ce58/initial] (3) [perdir /var/www/html/example/] applying pattern '^[a-z]{2}' to uri 'fr/blabla'
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b8ce58/initial] (2) [perdir /var/www/html/example/] rewrite 'fr/blabla' -> '/test.php?lang=fr'
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b8ce58/initial] (3) split uri=/test.php?lang=fr -> uri=/test.php, args=lang=fr
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b8ce58/initial] (1) [perdir /var/www/html/example/] internal redirect with /test.php [INTERNAL REDIRECT]
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b92310/initial/redir#1] (3) [perdir /var/www/html/example/] strip per-dir prefix: /var/www/html/example/test.php -> test.php
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b92310/initial/redir#1] (3) [perdir /var/www/html/example/] applying pattern '^[a-z]{2}' to uri 'test.php'
192.168.172.1 - - [24/Feb/2015:16:30:10 +0100] [example.com/sid#896a010][rid#8b92310/initial/redir#1] (1) [perdir /var/www/html/example/] pass through /var/www/html/example/test.php
What I don't understand is why, after the first match (4th line in the log), mod_rewrite keeps matching and redirecting again and again. I found several people posting similar questions here, and as a result of their answers, I tried adding the RewriteCond rule, but mod_rewrite seems to ignore it (yes, test.php does exist in my filesystem). What's going on?
EDIT: forgot to mention. The server I'm testing this on is an Apache 2.2.3.
EDIT: nevermind. It was a typo in my test.php, which was preventing it from being executed correctly (can you add here one of those "eyeroll" or "embarrassed" emojis?)
To stop after first rewrite you can use:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[a-z]{2} test.php?lang=$0 [L,QSA]
%{ENV:REDIRECT_STATUS} is internal variable that is set to 200 after first rewrite by Apache.
However RewriteCond %{REQUEST_FILENAME} !-f should stop rewrite loop after first rewrite if test.php is a valid file.
I want to redirect the following URLs:
From: http://www.example.com/13-articlename
TO: http://www.example.com/articlename
And I have following code:
RewriteCond %{QUERY_STRING} id=13
RewriteRule (.*)$ http://www.example.com/articlename [R=301,L]
In your rewrite you are expecting a querystring parameter of id however in your example it is actually part of the URL.
RewriteEngine on
RewriteBase /
RewriteRule (\d+)-([^/]*) $2 [R=301,L]
(\d+) = match any digits
- = a hyphen
([^/]*) = any characters except a forward slash
$2 = redirect to the second matching group - ([^/]*)
[R=301] = use a HTTP 301 redirect (omit if you want to have it rewrite instead of redirect)
[L] = Last rule (don't process following rules)
You can test at http://htaccess.madewithlove.be/
input url
http://www.example.com/13-articlename
output url
http://www.example.com/articlename
debugging info
1 RewriteEngine on
2 RewriteBase /
3 RewriteRule (\d+)-([^/]*) $2 [R=301,L]
This rule was met, the new url is http://www.example.com/articlename
Test are stopped, because of the R in your RewriteRule options.
A redirect will be made with status code 301
I have 2 domains that are displaying the same information. I would like to have the one that does not end in a slash 301 redirect to the one that does.
http://domain.com/locations/texas
http://domain.com/locations/texas/
Current Rewrite Rule:
RewriteRule ^locations/([a-zA-Z0-9_-]+)/?$ search.php?type=location&slug=$1 [L]
Change the rule you have no and remove the ? making the trailing slash a requirement:
RewriteRule ^locations/([a-zA-Z0-9_-]+)/$ search.php?type=location&slug=$1 [L]
Then add this (before or after):
RewriteRule ^locations/([a-zA-Z0-9_-]+)$ /locations/$1/ [L,R=301]
to redirect the browser when a request is made without the trailing slash.
I am using WAMP (apache 2.2.9 and PHP 5.4.17). I have few simple mod rewrite rules that are working on production server (CentOS) but on my windows machine one rule is being ignored. I spent 3 days on finding solution but none worked.
Here is mod rewrite rules
Options -Indexes FollowSymLinks MultiViews
RewriteEngine on
RewriteBase /mysite/
RewriteRule ^reviews/([a-zA-Z0-9_-]+)/([0-9]+)/(.*).html$ page-review.php?t=$1&m=$2&s=$3 [L] #This is being ignored by Apache
RewriteRule ^reviews/?$ reviews.php [L]
RewriteRule ^reviews/([a-zA-Z0-9_-]+)-reviews/?$ reviews.php?r=$1 [L]
RewriteRule ^file/([a-zA-Z0-9_-]+)/s([0-9]+)-([a-zA-Z0-9_-]+)/f([0-9]+)-(.*).html$ file.php?c=$1&s=$2&st=$3&&m=$4&t=$5 [L]
The first rewrite rule is being ignored by Apache. Surprisingly, when I remove all contents from htaccess it still reads my rules. I have turned on mod rewrite log and here is the log detail
log for http://localhost/mysite/reviews/product/11/samsung.html
127.0.0.1 - - [23/Aug/2013:14:37:34 +0100] [localhost/sid#453140][rid#1821000/subreq] (1) [perdir /mydir/] pass through /mydir/reviews.php
127.0.0.1 - - [23/Aug/2013:14:37:34 +0100] [localhost/sid#453140][rid#17cc4f0/initial] (1) [perdir /mydir/] pass through /mydir/reviews.php
127.0.0.1 - - [23/Aug/2013:14:37:34 +0100] [localhost/sid#453140][rid#17d0500/initial] (1) [perdir /mydir/] pass through /mydir/reviews.php
127.0.0.1 - - [23/Aug/2013:14:37:34 +0100] [localhost/sid#453140][rid#1835050/initial] (1) [perdir /mydir/] pass through /mydir/reviews.php
As you can see above, instead of rewriting above request to page-review.php it reads reviews.php file
Here is another request which works fine
log for http://localhost/mysite/file/downloads/software/f5983-docs.html
127.0.0.1 - - [23/Aug/2013:14:39:42 +0100] [localhost/sid#453140][rid#47f4188/initial] (2) [perdir /mydir/] rewrite 'file/downloads/software/f5983-docs.html' -> 'file.php?c=downloads&s=45&st=software&&m=5983&t=docs'
127.0.0.1 - - [23/Aug/2013:14:39:42 +0100] [localhost/sid#453140][rid#47f4188/initial] (2) [perdir /mydir/] trying to replace prefix /mydir/ with /mysite/
127.0.0.1 - - [23/Aug/2013:14:39:42 +0100] [localhost/sid#453140][rid#47f4188/initial] (1) [perdir /mydir/] internal redirect with /mysite/file.php [INTERNAL REDIRECT]
127.0.0.1 - - [23/Aug/2013:14:39:42 +0100] [localhost/sid#453140][rid#47ed3e0/initial/redir#1] (1) [perdir /mydir/] pass through /mydir/file.php
Rules mostly look alright. Try this slightly modified code:
Options -Indexes +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /mysite/
RewriteRule ^reviews/(\w+)/([0-9]+)/([^.]+)\.html$ page-review.php?t=$1&m=$2&s=$3 [L,QSA,NC]
RewriteRule ^reviews/?$ reviews.php [L,NC]
RewriteRule ^reviews/(\w+)-reviews/?$ reviews.php?r=$1 [L,QSA,NC]
RewriteRule ^file/(\w+)/s([0-9]+)-(\w+)/f([0-9]+)-([^.]+)\.html$ file.php?c=$1&s=$2&st=$3&&m=$4&t=$5 [L,QSA,NC]
Btw regex in your last rule won't match file/downloads/software/f5983-docs.html
I am moving my site from Mediawiki to Wordpress and would like to redirect this page:
http://wecheck.org/wiki/Aaron_Swartz
to this page:
http://newslines.org/wiki/category/computer-people/aaron-swartz/
Currently in .htaccess I have
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^title=Aaron_Swartz$
RewriteRule ^/w/index\.php$ http://newslines.org/wiki/category/computer-people/aaron-swartz/? [L,R=301]
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]
RewriteRule ^/?$ %{DOCUMENT_ROOT}/w/index.php [L]
The second part makes the pretty URLs for mediawiki. I have tried many, many variations but I can't get it to work at all. Any help most appreciated.
UPDATE: Log file using the solution given. What is the .phtml?
[24/Jan/2013:22:01:00 +0000] init rewrite engine with requested uri /wiki/Aaron_Swartz
[24/Jan/2013:22:01:00 +0000] (1) pass through /wiki/Aaron_Swartz
[24/Jan/2013:22:01:00 +0000] (1) [perdir /var/www/] pass through /var/www/w/wiki.phtml
[24/Jan/2013:22:01:00 +0000] (3) [perdir /var/www/] add path info postfix: /var/www/w/wiki.phtml -> /var/www/w/wiki.phtml/Aaron_Swartz
[24/Jan/2013:22:01:00 +0000] (3) [perdir /var/www/] strip per-dir prefix: /var/www/w/wiki.phtml/Aaron_Swartz -> w/wiki.phtml/Aaron_Swartz
[24/Jan/2013:22:01:00 +0000] (3) [perdir /var/www/] applying pattern '^wiki/Aaron_Swartz$' to uri 'w/wiki.phtml/Aaron_Swartz'
[24/Jan/2013:22:01:00 +0000] (3) [perdir /var/www/] add path info postfix: /var/www/w/wiki.phtml -> /var/www/w/wiki.phtml/Aaron_Swartz
[24/Jan/2013:22:01:00 +0000] (3) [perdir /var/www/] strip per-dir prefix: /var/www/w/wiki.phtml/Aaron_Swartz -> w/wiki.phtml/Aaron_Swartz
[24/Jan/2013:22:01:00 +0000] (3) [perdir /var/www/] applying pattern '^w/index\.php$' to uri 'w/wiki.phtml/Aaron_Swartz'
[24/Jan/2013:22:01:00 +0000] (1) [perdir /var/www/] pass through /var/www/w/wiki.phtml
Remember that Apache directives like RewriteRule are applied before MediaWiki even sees the request. Thus, your current rule should work for http://wecheck.org/w/index.php?title=Aaron_Swartz, but not for http://wecheck.org/wiki/Aaron_Swartz.
Actually, though, the rule won't work because your regexp begins with a /, but in .htaccess context the leading slash (or whatever you've set RewriteBase to) is removed before the rewrite rules are applied.
Thus, fixing those two problems, what you need is something like this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# match the short URL of the page:
RewriteRule ^wiki/Aaron_Swartz$ http://newslines.org/wiki/category/computer-people/aaron-swartz/ [R=301,L]
# optional: also match the long version of the URL:
RewriteCond %{QUERY_STRING} ^title=Aaron_Swartz$
RewriteRule ^w/index\.php$ http://newslines.org/wiki/category/computer-people/aaron-swartz/ [R=301,L]
Edit: Based on your log file, it looks like you have a wiki.phtml file in your webserver root, to which Apache is automatically resolving any URL paths beginning with /wiki/.
One workaround would be to move your rewrite rules to the main Apache config where they'll run before any such mapping is done; another, more straightforward way would be to just change the first rewrite rule above to:
# match the short URL of the page:
RewriteRule ^wiki\.phtml/Aaron_Swartz$ http://newslines.org/wiki/category/computer-people/aaron-swartz/ [R=301,L]
or even:
# match the short URL of the page:
RewriteRule ^wiki(\.phtml)?/Aaron_Swartz$ http://newslines.org/wiki/category/computer-people/aaron-swartz/ [R=301,L]