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]
Related
[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]
I am sure I am doing something silly here, but I can't seem to figure out what it is.
I can't understand why the RewriteCond for the {REQUEST_URI} isn't evaluating to true and thus preventing the RewriteRule from firing. Instead, given the following url, the rule fires and the user is always taken to https://www.diffsite.com.
Any help would really be appreciated - and thanks in advance.
url:
http://www.regularsite.com/page-name/
.htaccess code:
# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} (iphone|ipod|android|symbian|windows\phone|blackberry|iemobile|opera\ mobile|palmos|webos|googlebot-mobile) [NC]
RewriteCond %{REQUEST_URI} !^page-name/ [NC]
RewriteRule ^(.*)$ https://www.diffsite.com/ [L,R=302]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
%{REQUEST_URI} always matches a URI that starts with leading forward slash / so this will work for you:
RewriteCond %{REQUEST_URI} !^/page-name/ [NC]
However in your rule RewriteCond %{REQUEST_URI} is not even needed as you can do this negation in RewriteRule itself:
RewriteCond %{HTTP_USER_AGENT} (iphone|ipod|android|symbian|windows\phone|blackberry|iemobile|opera\ mobile|palmos|webos|googlebot-mobile) [NC]
RewriteRule !^page-name/ https://www.diffsite.com/ [NC,L,R=302]
Not that inside .htaccess leading slash is not matched in RewriteRule.
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]
How to redirect "www.mydomain.com/pattern/pattern/value" to "www.mydomain.com/pattern/value" ?
Using:
RewriteRule ^pattern/(.*) /$1 [l,r,qsa]
Redirects to: www.mydomain.com/value, missing the pattern directory.
It is because rule is executing twice due to this pattern: ^pattern/(.*)
It matches /pattern/pattern/value and redirects to /pattern/value
It matches /pattern/value again and redirect to /value
Replace your rule with this:
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]+/(.+)$ /$1?r [L,R]
Thanks anubhava on pointing me to the right direction:
This is my final code
RewriteRule ^search/(.*) search.php?q=$1 [qsa]
RewriteRule ^search/images/(.*) images/$1
RewriteRule ^search/css/(.*) css/$1
RewriteRule ^search/search/(.*) /search/$1?s [l,r]
RewriteCond %{HTTP_REFERER} search/(.*)
RewriteCond %{QUERY_STRINg} !^s$
RewriteRule ^search/(.*) /$1 [l,r]
I'm adding the ?s QUERY_STRING only in case of double pattern, to keep it as clean as possible.
The HTTP_REFERER is used to apply the rule only if the link comes from the page with the query result.
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.