Problem:
I want to redirect from www.domain.com/index.php?mod=daily to www.domain.com/daily_rent
While redirecting from www.domain.com/advert.php?id=11 to www.domain.com/advert/11 works fine like this:
RewriteCond %{THE_REQUEST} advert\.php\?id=([0-9]+)\ HTTP/
RewriteRule ^advert\.php$ http://www.domain.com/advert/%1? [R=301,L]
the same rule
RewriteCond %{THE_REQUEST} index\.php\?mod=daily\ HTTP/
RewriteRule ^index\.php$ http://www.domain.com/daily_rent? [R=301,L]
redirects to www.domain.com/daily_rent and throws 404 error. What could be the reason?
UPD:
Redirecting from www.domain.com/index.php?mod=daily to www.domain.com/daily_rent works fine, thanks anubhava
In that case another problem, on similar pages i have more than one query params, like this(example):
www.domain.com/?mod=daily&room=1&area=bstn
so, i try use that rule
RewriteCond %{THE_REQUEST} \?mod=daily&room=([0-9]+)&area=([A-Z]+)\ HTTP/
RewriteRule ^$ http://www.domain.com/daily_rent/room/%1/area/%2? [R=301,L]
RewriteRule ^daily_rent/room/([0-9]+)/area/([A-Z]+)/?$ /?mod=daily&room=$1&area=$2 [QSA,L]
what is the solution for that situation?
Thanks
Because you're missing internal rewrite rules. Try these rules:
RewriteCond %{THE_REQUEST} advert\.php\?id=([0-9]+)\s
RewriteRule ^advert\.php$ /advert/%1? [R=301,L]
RewriteCond %{THE_REQUEST} index\.php\?mod=(daily)\s
RewriteRule ^index\.php$ /%1_rent? [R=301,L]
RewriteRule ^(daily)_rent/?$ /index.php?mod=$1 [QSA,L]
RewriteRule ^advert/([^/]+)/?$ /advert.php?id=$1 [QSA,L]
Surely you want it the other way round - to redirect /daily_rent to /index.php?mod=daily? That would be a more common usage or mod_rewrite.
RewriteRule ^daily_rent$ /index.php?mod=daily [R=301,L]
You don't need the RewriteCond
Related
I would like to rename url www.mypage.com/index.php or www.mypage.com/index_cz.php (the same version of index - just in another language) to www.mypage.com/en or www.mypage.com/cz
I have this code in .htaccess
RewriteEngine On
RewriteRule ^index\.php$ /en/ [R=301,L]
RewriteRule ^index_cz\.php$ /cz/ [R=301,L]
It redirects me to www.mypage.com/en or www.mypage.com/cz but it gimme back NOT FOUND...
Really thanks for ur help
You can use:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php[?/\s]
RewriteRule ^index\.php$ /en/ [R=301,L]
RewriteCond %{THE_REQUEST} /index_cz\.php[?/\s]
RewriteRule ^index_cz\.php$ /cz/ [R=301,L]
RewriteRule ^/en/?$ index.php [NC,L]
RewriteRule ^cz/?$ index_cz.php [NC,L]
Below are the things i need to convert using .htaccess
If url is sample.xxx.com means i need to convert it to www.xxx.com/domain/sample
I url is sample.xxx.com/category/34/electonics.html means i need to convert it to www.xxx.com/domain/sample/category/34/electronics.html
3.After converting those things again i need to build request to the necessary pages
I have the following rule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/domain/%1/$1 [P,QSA]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)$ index.php?domain=$1 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/$ index.php?domain=$1 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/news/([a-zA-Z0-9_\-]+)/([0-9]+)_([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ news.php?domain=$1&category=$4&news=$3 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/category/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ category.php?domain=$1&category=$2 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/gallery/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ gallery.php?domain=$1&category=$2 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/video/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ video.php?domain=$1&category=$2 [Nc,L]
But it fails
If i change line 4 to
RewriteRule ^(.*)$ http://www.example.com/domain/%1/$1 [NC,QSA]
means it will work but the browser shows the reconstructed url.
Anyone spot my error please.
If you keep http:// in target URI then it will indeed be full redirect.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|ftp|mail)\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^((?!domain/).*)$ domain/%1/$1 [L,NC]
RewriteRule ^domain/([\w-]+)/news/([\w-]+)/([0-9]+)_([0-9]+)/([\w-]+)\.html$ news.php?domain=$1&category=$4&news=$3 [NC,L]
RewriteRule ^domain/([\w-]+)/category/([0-9]+)/([\w-]+)\.html$ category.php?domain=$1&category=$2 [NC,L]
RewriteRule ^domain/([\w-]+)/gallery/([0-9]+)/([\w-]+)\.html$ gallery.php?domain=$1&category=$2 [NC,L]
RewriteRule ^domain/([\w-]+)/video/([0-9]+)/([\w-]+)\.html$ video.php?domain=$1&category=$2 [NC,L]
RewriteRule ^domain/(.+)/?$ index.php?domain=$1 [NC,L]
I am changing my site URLs with .htaccess.
Until now, I was using
RewriteRule ^folder/([^/.]+)/p([0-9]+)/?$ folder/handler.php?name=$1&p=$2 [L]
Now, I want to change the "/p" for "-p".
I tried
RewriteRule ^folder/([^/.]+)/p([0-9]+)/?$ ^folder/([^/.]+)-p([0-9]+)/?$ [L, R=301]
But it gives me a 500 error when I try to access any of the two urls.
Is there any way to make a 301 from the first to the second one? Or any other choice?
Couple of issues:
You have space before R=301
On RHS (target side) of RewriteRule you have a regex
Keep your rules like this:
RewriteRule ^(folder/[^/.]+)/(p[0-9]+/?)$ /$1-$2 [L,NC,R=301]
RewriteRule ^folder/([^.-]+)-p([0-9]+)/?$ folder/handler.php?name=$1&p=$2 [L,NC,QSA]
This is how i do:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www*******com$ [NC]
RewriteRule ^(.*)$ www.******.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ www.*******.com/ [R=301,L]
Redirect /..../p /....-p
I'm trying to change my .htaccess file so that if I go to:
http://www.example.com/index.php?login=true, it goes to http://www.example.com/login.
I currently have this code which removes index.php (which makes the above looks like http://www.example.com/?login=true).
RewriteEngine On
#remove index.php
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
RewriteCond %{THE_REQUEST} ^GET
I would setup your rewrite rule the other way around:
RewriteEngine On
RewriteRule ^login$ /index.php?login=true
This way if a user browses to http://yourserver.com/login the actual page used is http://yourserver.com/index.php?login=true, but the first URL is shown in the browser. I assume this is what you are trying to achieve.
If you really need to do it in the direction you asked for, you can try something like this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^login=true$
RewriteRule ^index\.php$ /login [L,R=301]
This will fail of there are additional query parameters.
If you want to redirect http://yourserver.com/index.php to http://yourserver.com you can simply add the following rewrite rule:
RewriteRule ^index\.php$ / [L,R=301]
The following should work although there might something wrong with the line to exclude /system/*. Try testing with that commented out.
RewriteEngine On
RewriteCond %{QUERY_STRING} ([^=]*)=true [NC]
RewriteCond %{THE_REQUEST} ^/system/.*
RewriteRule index.php /%1? [R=301,L]
The following page helped: http://wiki.apache.org/httpd/RewriteQueryString
This tester is cool but does have a few bugs in it: http://martinmelin.se/rewrite-rule-tester/
try the following in the .htaccess in root directory of example.com
RewriteEngine On
RewriteBase /
#rewrite http://www.example.com/anything to http://www.example.com/index.php?anything=true
RewriteCond %{REQUEST_URI} ^/([-a-zA-Z0-9]+)$ [NC]
RewriteCond %1 !system [NC]
RewriteRule . index.php?%1=true [L]
#301 redirect requests for example.com/index.php?anything=true to example.com/anything
RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteCond %{QUERY_STRING} ^([^=]+)=true$ [NC]
RewriteRule . /%1? [L,R=301]
You need to check for the QUERY_STRING in a RewriteCond. I think this should do:
RewriteEngine On
#remove index.php
RewriteCond %{QUERY_STRING} ([^=]*)=true [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule ^index\.php /%1? [R=301,L]
This should redirect anything which has index.php?=true to /action
I have a search form that sends a GET request to a page called search.php. I have rewrite rules set up in my htaccess file that rewrite certain things, like /search, to their respective pages. I simply want to take the search.php?q=query and rewrite it to /search/query.
Here is what I have.
RewriteRule search.php?q=(.*) /search/$1
RewriteRule search/(.*) search.php?q=$1 [nc]
What am I doing wrong?!
Here is the complete file
ErrorDocument 404 /index.php?p=404
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/.*\.jpg$ /images/default.jpg [L]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?wghandcrafted.com/.*$ [NC]
RewriteRule \.(gif|jpg|jpeg|psd|js|swf|flv|png)$ /feed/ [R=302]
RewriteRule ^(products|blog|feed|search|checkout|checkout)$ $1.php [nc]
RewriteRule products/cat/(.*)$ products.php?type=cat&cat=$1 [nc]
RewriteRule products/(.*)$ products.php?type=single&product=$1 [nc]
RewriteRule blog/(.*) blog.php&post=$1 [nc]
RewriteRule feed/(.*) feed.phptype=$1 [nc]
RewriteRule search\.php?q=(.*)$ /search/$1 [R=301,L]
RewriteRule search/(.*)$ search.php?q=$1 [NC]
Make the first line perform a Redirect and the second perform a Rewrite
RewriteRule search\.php?q=(.*)$ /search/$1 [R=301,L]
RewriteRule search/(.*)$ search.php?q=$1 [NC]
and move
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/.*\.jpg$ /images/default.jpg [L]
to the end of the set of rules.
Otherwise, the RewriteCond %{REQUEST_FILENAME} !-f rule is enacted before anything else, meaning that only requests for non-existant files will be handled by any rules below that line. As there is a search.php file, this prevents that rule from ever being reached.
I was having the same problem and here is a solution i found that worked for me, on my site the queries are being sent to index.php, I discovered if I had "^index.php$ /search/%1? [R=301]" as the first rewrite rule it will just error out because of the second rewrite rule making it just go in a loop so i replaced "^index.php$" with "^$" allowing it to still request the same file. It might not be the best solution, but a work around that works. Here is my working code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=(.*)$
RewriteRule ^$ /search/%1? [R=301]
RewriteBase /search
RewriteRule ^search/(.*)$ /index.php?q=$1 [NC]