Having an issue where the URL rewrite is running but the server will not display the contents. My .htaccess file is
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /project\.php [NC]
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule . project-%1.html? [R=301]
Thanks for your help!
EDIT: Sorry, here is some more info!
I want to rewrite
www.siteurl.co.uk/project.php?id=82
to
www.siteurl.co.uk/project-82.html
The code in the .htaccess rewrites the URL perfectly but the page does not display. I get a
404 The requested URL /project-82.html was not found on this server.
I hope that helps!
You only have one half of the solution, creating the pretty urls. What is missing is the other half, sending the pretty urls to an application for processing as below
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /project\.php [NC]
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule . project-%1.html? [R=301]
#you need this rule to process your www.siteurl.co.uk/project-82.html request
RewriteRule ^project-([0-9]+)\.html$ project.php?id=$1 [L,NC,QSA]
try putting RewriteRule ^project-%1.html? [R=301] instead of the .
Don't know what the "[A-Z]{3-9}\ " is for? maybe I'm missing something?
But maybe something like this instead:
RewriteCond %{THE_REQUEST} ^project\.php [NC]
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule . project-%1.html? [R=301]
or
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^/project\.php project-%1.html? [R=301]
Note that this is just off the top of my head, and I didn't test it.
EDIT: Just adjusted the second example which had an extra slash in it, and tested it out on my webserver (note that I'm using IIS/URLRewrite, but should be same):
RewriteBase /
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^project\.php project-%1.html? [R=301]
I entered http://localhost/project.php?id=123 and it went to http://localhost/project-123.html
But I think I missed something... you say the url rewrite is working, and the url is changing but you're getting a 404? In that case then your rewrite sounds fine - and something else is not permitting your html files to be accessed (or they don't exist)?
Related
So i have a htaccess rewrite that makes a query string to a path like so..
RewriteRule ^page/(.*)$ page.php?query=$1
This works fine and using the above i can access the page via both page.php?query= and /page/query/ but how can i make it so that if the page is accessed by the page.php?query= method, it automatically redirects to the path version?
I've tried the following but i don't think i wrote it correctly...
RewriteCond %{HTTP_HOST} ^www\.webaddress\.com\/page\.php\?query\=$
RewriteRule ^(.*)$ http://www.webaddress.com/page/$1 [R=301,L]
Any help to fix this would be appreciated. Thanks.
You can use the following
RewriteEngine On
#url redirection from old url to the new one
#redirect /page.php?query=foo to /page/foo
RewriteCond %{THE_REQUEST} /page.php\?query=([^\s]+) [NC]
RewriteRule ^.+$ /page/%1? [L,R]
#url rewiring from new url to the old one
# rewrite /page/foo to /page.php?query=foo
RewriteRule ^page/(.*)$ page.php?query=$1 [L]
Try it before your internal rewrite rule.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.+)\.php
RewriteCond %{QUERY_STRING} ^(.+)\=(.+)
RewriteRule ^ http://%{HTTP_HOST}/%1/%2/%3? [R=301]
from
cdn1.example.com
cdn2.example.com
cdn3.example.com
to
example.com
Please let me know how to redirect those?....
You could have provided more details and also what you have tried, anyways here you go:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(cdn1|cdn2|cdn3)\.example\.com
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
As a bonus, any eventual path and/or querystring is preserved, I guess you would need that.
EDIT : As you requested, here is a cdnx version:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^cdn[0-9]+\.example\.com
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
I have an URL as :
http://mydomain.com/site/?cmd=home
I want to change the above address to http://mydomain.com/home
i am using .htaccess file like this:
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /site?cmd=$1 [L]
I am not sure where is the problem?
Thanks in advance
The Rule works the other way around: you want to get from /site/?cmd=home to /home, but with your RewriteRule you are redirecting to /site?cmd= (Probably there is a mistake somewhere?)
If you want to redirect from /site/?cmd=home to /home, like the question states, use the following:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/site
RewriteCond %{QUERY_STRING} ^cmd=(.*)$
RewriteRule ^(.*)$ /%1/? [R=302,L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} /site\?cmd=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^([^/.]+)/?$ /site?cmd=$1 [L]
I have to redirect url
http://www.nv-pedia.de/pedia/?q=node/630
to
http://www.nv-pedia.de/hafenplan-s256-arsdale/
My solutions doesn´t do anything and unfortunately i have not found an answer on this.
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)630(&|$) [NC]
RewriteRule $/hafenplan-s256-arsdale/? [R=301,L]
You can use:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)q=node/630(&|$) [NC]
RewriteRule ^pedia/?$ /hafenplan-s256-arsdale/? [R=301,L,NC]
Currently I have this rewrite rule in place and it works great:
RewriteRule ^search/([^/]+)/? search.php?term=$1 [NC,QSA]
When I go to mysite.com/search/foo it takes me to the proper search page for foo. what I am looking to do is make the opposite work as well so that if I visit mysite.com/search.php?term=foo I will be taken to mysite.com/search/foo. I think I need a 301 redirect somewhere but I'm not sure how to do it without creating an infinite loop and the other posts I've read so far haven't been much help.
Try:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /search\.php\?term=([^&]+)
RewriteRule ^search\.php$ /search/%1/? [L,R=301]
or (if you place these after the rule that you already have
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^term=([^&]+)$
RewriteRule ^search\.php$ /search/%1/? [L,R=301]
Try this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+mysite.com/search.php?term=foo [NC]
RewriteRule ^ mysite.com/search/foo [L,R=301]
Add the R flag to your RewriteRule:
RewriteRule ^search/([^/]+)/? search.php?term=$1 [NC,QSA,R=301]