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.
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]
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]
I have tried almost every possible "solution" that I could find online for this matter, including Stackoverflow.
I am trying to rewrite
http://example.com/index.php?view=search&q=something
to
http://example.com/search/?q=something
When searching for something, the searchform direct's you to
search/?q=this
But "/search/" is allread passed once so it has already used the questionmark
now i'm stuck with the script redirecting every search with a questionmark while it should be an "&" sign.
It works if I manualy change the ? to &.
Can someone pleas help me make it work with the Questionmark?
Here is a copy of my .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
ErrorDocument 404 /test.php
RewriteRule ^nothingfound/ test.php [L]
RewriteCond %{QUERY_STRING} q=([^&]+)
RewriteCond %{QUERY_STRING} v=([^&]+)
RewriteRule ^search/([^/]+) /index.php?view=search&q=%1
RewriteRule ^([^/.]+)/?$ index.php?view=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)/?$ index.php?view=$1&task=$2 [L]
RewriteRule ^([^/.]+)/([0-9]+)/([^/.]+)*/$ index.php?view=$1&id=$2&title=$3 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([0-9]+)*/$ index.php?view=$1&task=$2&page=$3 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/*/$ index.php?view=$1&task=$2&letter=$3 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/([0-9]+)*/$ index.php?view=$1&task=$2&letter=$3&page=$4 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/([0-9]+)/*/$ index.php?view=$1&id=$2&title=$3&page=$4 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/*/$ index.php?view=$1&id=$2&title=$3&list=$4 [L]
Thanks in advance.
This rule should work for you:
RewriteCond %{QUERY_STRING} (^|&)[qv]=.+ [NC]
RewriteRule ^(search)/?$ index.php?view=$1 [QSA,L,NC]
Is this what you're looking for?
Replace this
RewriteCond %{QUERY_STRING} q=([^&]+)
RewriteCond %{QUERY_STRING} v=([^&]+)
RewriteRule ^search/([^/]+) /index.php?view=search&q=%1
with this
RewriteCond %{QUERY_STRING} \bq=
RewriteRule ^search/$ index.php?view=search [QSA,L]
I'm setting up 301 Redirects in my .htaccess and 1 of them is:
Redirect 301 /article_list.php?parent_cat=152&catid=187 http://mysitehere.com/resources/objects/tribulus/
When I double check it by going to http://mysitehere.com/article_list.php?parent_cat=152&catid=187 it just stays there on that page and it says 404: Page Not Found. Did I do this incorrectly?
You need mod_rewrite rule to match query string. Consider this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^parent_cat=152&catid=187$ [NC]
RewriteRule ^article_list\.php$ /resources/objects/tribulus/? [R=301,L,NC]
Reference: Apache mod_rewrite Introduction
UPDATE:: Your full .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^parent_cat=152&catid=187$ [NC]
RewriteRule ^article_list\.php$ /resources/objects/tribulus/? [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1/ [L]
I believe the Redirect directive of the mod_alias module doesn't really handle/match query strings at all, cf. its documentation:
mod_alias is designed to handle simple URL manipulation tasks. For more
complicated tasks such as manipulating the query string, use the tools provided
by mod_rewrite.
For matching a query string, I suggest you use a RewriteCond+RewriteRule combination, where the RewriteCond matches your %{QUERY_STRING} lexicographically. :)
I am trying to serve different urls using mod_rewrite but whatever I try it is just not working.
An example url would be
http://www.site.com/country/tours/dynamic-part/?&city=new-york,los-angeles
And I am trying to change the url using .htaccess to:
http://www.site.com/country/tours/dynamic-part/new-york,los-angeles
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/.+city=([^\/]*)$ http://www.site.com/country/tours/$1/$2 [L,R=301]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Any ideas? I though I was close but not anymore :/
The RewriteRule does NOT match the query string, see
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#what_is_matched
So the .+city part of the rule will never match.
This should work tho...
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/ http://www.site.com/country/tours/$1/%2 [L,R=301]
The subsitution can read back-referenecs to the RewriteCond pattern.