Rewriting search term in URL using htaccess and mod_rewrite - php

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]

Related

htaccess redirect query string to a path

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]

HTACCESS Redirection from subdirectory

i am Working on a redirect script
i want to redirect all links contaning /en/
For eg redirect link like this
http://example.com/en/some-text/123-some-text-123456.html
to
http://example.com/some-text/123-some-text-123456.html
I am trying with this script but not workin as of now
RewriteEngine on
RewriteCond %{HTTP_HOST} (.*)\en\(.*)
RewriteRule http://www.example.com/$1 [R=301,L]
Any help would be appreciated
Thanks!
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/en/(.*) [NC]
RewriteRule .* http://www.example.com/%1 [R=301,L]
Use this - with the %1 modifier you can access the extract group from the previous RewriteCond.
It is simple by using this
Redirect 301 http://example.com/en/(.*)$ http://example.com/$1

rewrite get parameters with htaccess

I am knocking my head how to rewrite get parameters with htaccess.
Here is my htaccess so far (removing only index.php).
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I want to rewrite the following url http://example.com/?lang=en&dscroll=1200 into http://example.com/en/dscroll/1200. Some htaccess master, please :)?
Thanks
The rule that you have doesn't remove the index.php, it only redirects to a host without the www in front.
As for the other rewrite, you need 2. First, you need to make sure your links in all of your content looks like this:
http://example.com/en/dscroll/1200
instead of the one that has the query string.
Then you need to add, below the rule that you already have, these 2 rules:
RewriteCond %{THE_REQUEST} \ /+\?lang=([^&]+)&([^=&\ ]+)=([^&\ ]+)
RewriteRule ^ /%1/%2/%3? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /?lang=$1&$2=$3 [L,QSA]

replacing Login URL to short URL with .htaccess

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]

htaccess URL rewrite - requested URL not found on server

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)?

Categories