so this is my url:
http://www.mysite.com/mypage.php?PropertyBuyRent=rent&Developer=&Resort=&City=&State=&Country=&Price=
I want to write the following htaccess:
if PropertyBuyRent=rent and Resort value is not empty then url should be like:
http://www.mysite.com/mypage.php/TimeshareForRent/Marriott's+Grande+Ocean+Resort (whatever value comes from the url for Resort)
You can use this rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(testing/mypage\.php)\?PropertyBuyRent=rent&Developer=&Resort=([^&]+)&City=&State=&Country=&Price= [NC]
RewriteRule ^ /%1/TimeshareForRent/%2? [R=301,L,NE]
RewriteRule ^(testing/mypage.php)/TimeshareForRent/([^/]+)/?$ /$1?PropertyBuyRent=rent&Developer=&Resort=$2&City=&State=&Country=&Price= [L,NC,QSA]
Related
I have a URL structure like this
localhost/sumar/gallery.php?gen=slug-here
that i want to clean up for example: localhost/sumar/gallery/slug-here
so i need rewrite rule for this condition and prevent anyone from directly hitting the base URL http://localhost/sumar/gallery.php?gen=slug-here by auto redirecting it to localhost/sumar/gallery/slug-here
So far i have tried this method:
//that works
RewriteRule ^Gallery/([A-Za-z0-9-_]+)/?$ gallery.php?gen=$1
//below one does not seems to work
RewriteCond %{THE_REQUEST} ^GET\ /sumar/gallery\.php\?gen=([\w-]+) [NC]
RewriteRule ^sumar/gallery\.php$ /sumar/Gallery/%1? [R=301,L]
You'd need to act on the raw requests:
RewriteEngine On
RewriteRule ^(sumar)/Gallery/([\w-]+)/?$ /$1/gallery.php?gen=$1
RewriteCond %{THE_REQUEST} ^GET\ /sumar/gallery\.php\?gen=([\w-]+) [NC]
RewriteRule ^sumar/gallery\.php$ /sumar/Gallery/%1? [R=301,L]
PS: The character set [A-Za-z0-9_] can be represented with \w.
Via .htaccess, I would like to create an automatic 301 from an old URL to a new url:
An example old url is: http://www.example.com/test.html?s=2&ss=3
I would like that to be automatically redirected to: http://www.example.com/test.html
If you want to match this specific URL and query parameters then you can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=2&ss=3^ [NC]
RewriteRule ^test\.html$ %{REQUEST_URI}? [L,R=302]
If you want to use this query string with any URI then use:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=2&ss=3^ [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R=302]
? after %{REQUEST_URI} is needed to strip off any query string.
What am trying to do in that code is:
Any visitor click on an href like http://domain.com/article.php?id=44 the .htaccess will transfer him to domain/article/44
then another rewrite rule to to get the content of requested id from the file article.php
RewriteCond %{QUERY_STRING} ^id=([^&]*)
RewriteRule ^article\.php$ http://alarabe.org/article/%1?
RewriteRule ^article/([a-z0-9\-]+)$ /article.php?id=$1 [L]
This rule isn't right and will certainly cause looping. You need to use THE_REQUEST variable that represents original request received by Apache from your browser.
Try this instead:
RewriteCond %{THE_REQUEST} \s/+article\.php\?id=([^\s&]+) [NC]
RewriteRule ^ http://alarabe.org/article/%1? [R=302,L]
RewriteRule ^article/([a-z0-9\-]+)$ /article.php?id=$1 [L]
Example:
1) URL (a poorly constructed one) is already indexed by Google:
www.abc.com/index.php?product=zzz
2) The URL is rewritten by using the following rewrite rule:
RewriteRule ^zzz$ index.php?product=zzz [L,NC]
The above is working fine, but we want to tell Google that the page has permanently moved from the URL in (1) URL www.abc.com/index.php?product=zzz to the URL in (2) www.abc.com/zzz
3) So now we apply the following rule above the rule (2):
RewriteCond %{QUERY_STRING} ^product=zzz [NC]
RewriteRule index http://www.abc.com/zzz? [L,R=301]
This results in an infinite loop. How do we tell Google that our site has changed from www.abc.com/index.php?product=zzz to www.abc.com/zzz? Or will Google do this by themselves?
-----Current full htaccess rules resulting in infinite loop:-----
RewriteCond %{QUERY_STRING} ^product=zzz [NC]
RewriteRule index http://www.abc.com/zzz? [L,R=301]
RewriteRule ^zzz$ index.php?product=zzz [L,NC]
Replace your existing rules with this code:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?product=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^(zzz)/?$ /index.php?product=$1 [L,NC,QSA]
I'm trying to add one last improvement (regarding htaccess), and that is that I would like it to redirect /?mod=inicio to /inicio. So I'm trying to do it with the following code, but It keeps building the url like this /inicio?mod=inicio
RewriteCond %{QUERY_STRING} ^mod=([a-z]+)$ [NC]
RewriteRule .* /%1 [L]
Same thing with extra parameters: from /?mod=x&type=y-z to /x/y-z
Try these rules instead:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?mod=([a-z]+)&type=([a-z\-]+)
RewriteRule ^ /%1/%2? [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?mod=([a-z]+)($|\ )
RewriteRule ^ /%1? [L]
The key is to match against the actual request instead of the URI because your other rules (from your previous question) will cause these rewrites to match, they'll conflict with each other. The other key point is to include a ? at the end of the targets (e.g. /%1/%2?) which makes it so the query string doesn't get appended.