I have the following rewrite rules:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$
RewriteCond %{REQUEST_URI} !^/(Account|Logout|Password|Tags) [NC]
RewriteRule ^([^/]*)$ /index.php?city=$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/Account [NC]
RewriteRule ^Account /members/account.php [NC,L]
RewriteCond %{REQUEST_URI} ^/Logout [NC]
RewriteRule ^Logout /members/logout.php [NC,L]
RewriteCond %{REQUEST_URI} ^/Password [NC]
RewriteRule ^Password /members/password.php [NC,L]
RewriteCond %{REQUEST_URI} ^/Tags [NC]
RewriteRule ^Tags /members/tags.php [NC,L]
I'm trying to add another condition that so that it loads as
domain.com/$city/$provider/$name
this is the rule:
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /listing.php?city=$1&provider=$2&urlname=$3 [L]
My issue is that this rule conflicts with the rule for my index file which loads as domain.com/$city.
I'd greatly appreciate any and all suggestions.
Thanks!
There's nothing wrong with any of your rules. Its just that the first covers the last in terms of what it will accept. The easiest way to avoid this problem is to move the more restrictive rule above of the more general. I.e. in this case put the Listing rule immediately before the index.php rule.
Addendum
Oops, The ->listing.php followed by the ->indexp.php rules will still loop because rule1 fires on the first pass and then rule2 on the second, since the query string is stripped for purposes of regexp matching in a rule; and "listing.php matches ^([^/]*)$.
You should only match ^([^/]*)$ in the case where the pattern doesn't match an existing file. The way to prevent this is to put a
RewriteCond %{REQUEST_FILENAME} !-f
before the index.php rule.
Related
I have two different URLs:
1.: mysite.com/file.php
2.: mysite.com/**articles**.php?article=**something**
I would like to rewrite mysite.com/file.php to mysite.com/file.
AND
I would like to rewrite mysite.com/**articles**.php?article=something to mysite.com/**news**/**something**
I tried this but it's not working.
It rewrite mysite.com/file.php to mysite.com/file, but does not rewrite mysite.com/**articles**.php?article=**something** to mysite.com/**news**/**something**
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?\ ]+)\.php
RewriteCond %{REQUEST_FILENAME} !exception.php
RewriteRule ^news/([^/]+)(/*)$ /articles.php?article=$1
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
(There is one exception in the code: exception.php which I don't want to rewrite.)
Can you please correct my code? I've been working on this for 4 days, I've read everything, but I can't do it.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect for file.php file.
RewriteCond %{REQUEST_URI} !/?exception\.php [NC]
RewriteCond %{THE_REQUEST} \s/(file)\.php/?\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite for php files.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ $1.php [QSA,L]
##external + internal rewrite rules for articile.php here.
RewriteCond %{THE_REQUEST} \s/articles\.php\?article=(\S+)\s [NC]
RewriteRule ^ /news/%1? [R=301,L]
RewriteRule ^news/([^/]*)$ articles.php?article=$1 [NC,QSA,L]
[Edited code]*
This is the current code I am using
Options +FollowSymLinks
RewriteCond %{REQUEST_URI} (bus/(.*))
RewriteRule bus/(.*) page1.php?param=$1 [L,NC,QSA]
RewriteCond %{REQUEST_URI} (car/(.*))
RewriteRule car/(.*) test/page2.php?param=$1 [L,NC,QSA]
RewriteCond %{REQUEST_URI} (bike/(.*))
RewriteRule bike/(.*) test/page3.php?param=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/(.+)$ new.php?param1=$1¶m2=$2 [L,QSA]
I am trying to get the wild card condition in the last line if the above conditions fail. But the server returning with the 404. Its not catching the condition.
Any Help would be appreciated.
Keep all specific rules are top and 3rd generic catch-all rule at the last:
RewriteEngine On
RewriteRule ^bus/(.+)$ page1.php?param=$1 [L,NC,QSA]
RewriteRule ^car/(.+)$ test/page2.php?param=$1 [L,NC,QSA]
RewriteRule ^bike/(.+)$ test/page3.php?param=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/(.+)$ new.php?param1=$1¶m2=$2 [L,QSA]
Two RewriteCond are required before last rule to prevent rewriting actual files and directories.
Your need to use the L flag at the end of your rules to stop multiple rule processing :
RewriteRule ^ask/(.*) forum.php?param=$1 [L,NC]
RewriteRule ^city/(.*) city.php?param=$1 [L,NC]
We want to change links "www.example.com/page/login/" to "www.example.com/sayfa/giris". I tried this: RewriteRule ^page/^(.+[^/])$/ /sayfa/^(.+[^/])$ [L] But nothing is change. Is there any error?
Thank you very much for your response #geert3 and #anubhava. But all trying failed. Here is my .htaccess content:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteRule /(uploads/.*) $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ page.php?url=$1 [QSA,L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteRule /(uploads/.*) $1 [R=301,L]
RewriteRule ^sayfa/giris/?$ /page/login [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ page.php?url=$1 [QSA,L]
Make sure there is not .htaccess in /sayfa/ folder.
I think you need to remove the second ^ and add an initial slash after the first ^. The slash after $ must be removed.
^/page/(.+[^/])$
^ at the start of an expression means "beginning of line" (i.e. in your case you want /page in front of the path). After a square bracket it means "none of the listed characters" (i.e. in your case no slash). In other places, it makes no sense.
$ means "end of line" so it makes no sense to add stuff after it.
Also as #Croises noted, you can't have "matches" on the right. Right side is your target URL, so there you can either re-use matches you made on the left side, use environment variables or hardcoded text, but no new matches. So no ^, [], $ etc.
So for instance the whole rule would become:
^/page/(.+[^/])$ /sayfa/giris [L]
There are two things that I would like to achieve with .htaccess file.
The first one is
www.hostname.com/index.php?question -> www.hostname.com/question
www.hostname.com/index.php?myinfo -> www.hostname.com/myinfo
www.hostname.com/index.php?notification -> www.hostname.com/notification
so I use external rewrite to re-express on the URL as following.
RewriteCond %{THE_REQUEST} /(index.php)\?([^&]+)\ HTTP
RewriteRule ^ /%2? [R=301,L]
Now the above statement correctly displays as I want. The problem is to internally convert back when a condition is satisfied. The condition is if %{THE_REQUEST} is equal to any character after '/'.
RewriteCond %{REQUEST_URI} ^/(.*)$ [NC]
RewriteRule ^(.*)$ index.php?$1 [NC,L]
So that my php code can recognize the $_GET parameter. Here, even though the condition is satisfied it will not process the RewriteRule.
The second problem is
www.hostname.com/index.php?category=spo -> www.hostname.com/category/spo
www.hostname.com/index.php?category=ite -> www.hostname.com/category/ite
www.hostname.com/index.php?category=gam -> www.hostname.com/category/gam
The conversion is completed using exteral rewrite:
RewriteCond %{THE_REQUEST} /(index)\?category=([^&]+)\ HTTP
RewriteRule ^ category/%2? [R=301,L]
Again, I'd like to convert back whatever is written in the URL back to the original format internally so I use the following condition to differentiate from the previous case,
RewriteCond %{REQUEST_URI} ^/category/(.*)$
RewriteRule ^category/(.*)/?$ index.php?category=$1 [NC,L]
my php code cannot recognize the $_GET parameter and variable. When I use htacces tester, it says it should work but it doesn't. http://martinmelin.se/rewrite-rule-tester/
How do I fix this problem? OR is there any easier way to accomplish this?
Thank you
Try these rules:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/index\.php\?category=([^&\s]+) [NC]
RewriteRule ^ /category/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^/]+)/?$ /index.php?category=$1 [NC,L,QSA]
RewriteCond %{THE_REQUEST} \s/index\.php\?([^&\s]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?$1 [QSA,L]
I'm trying to handle multiple areas of an application, but the URLs are not being rewrited as expected.
This is the .htaccess file:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^division/(.*)/section/(.*)$ ./index.php?division=$1§ion=$2
RewriteRule ^division/(.*)$ ./index.php?division=$1
RewriteRule ^area/(.*)$ ./index.php?area=$1
What's expected:
If the URI matches division/some_division/section/some_section rewrite to index.php?division=some_division§ion=some_section
If no section (section/some_section) is defined, go to the second rule -
If the URI matches division/some_division rewrite only to index.php?division=some_division
If no division is defined, and the URI matches area/some_area rewrite to index.php?area=some_area
I'm almost sure I can combine the two first rules, I've tried this regex but it didn't work:
^division/(.*)( /section/(.*) )?$
It supposed to make /section/some_section an optional value.
Unfortunately nothing works. Any ideas?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^division/(.*)/section/(.*)$ ./index.php?division=$1§ion=$2 [QSA,L]
RewriteRule ^division/(.*)$ ./index.php?division=$1 [QSA,L]
RewriteRule ^area/(.*)$ ./index.php?area=$1 [QSA,L]