Rewriting Dynamic URLs in htaccess not working - .htaccess

I want to rewrite
mypage.com/country/country.php?country=something
to
mypage.com/country/something
in the address bar, using htaccess
I've tried many things and looked everywhere and the closest I've got is:
RewriteCond %{QUERY_STRING} country=([^\&]*)
RewriteRule ^country.php$ /country/%1? [R,L]
But this just produces a rewrite loop that alternates between the two links above and I don't understand why.
I want both
mypage.com/country.php?country=something
and
mypage.com/country/something
when entered, to show
mypage.com/country/something
in the address bar
Any help?

If you have a rewrite loop it suggests that besides that rule, you also have a rule that translates it back. You'll need the 'ugly' url to only trigger when it is an external request. The easiest way to do that is by matching %{THE_REQUEST}.
#Ugly to fancy url; should be R=301 when it works
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /country\.php\?country=([^\&]*)\ HTTP
RewriteRule ^country.php$ /country/%2? [R,L]
RewriteRule ^country/(.*)/?$ /country.php?country=$1 [L]

Related

htaccess - rewrite rule - redirect after url rewrite

This is pretty simple, but can't seem to figure this out.
I have a url /shows/index.php?id=1 that I wanted to appear in the url as /shows/1/index.php.
So I added this to my htaccess:
RewriteRule ^shows/([0-9]*)/index.php$ /shows/index.php?id=$1 [L]
This worked like a charm. I can now navigate to /shows/1/index.php no problem at all. However, I can also still navigate to /shows/index.php?id=1. I wanted that page to automatically redirect to the new url, so I wrote this:
RewriteRule ^shows/index.php?id=([0-9]*)$ /shows/$1/index.php [R=301,L]
...but it doesn't do anything. However, if I do something like:
RewriteRule ^shows/([0-9]*)/0$ /shows/$1/index.php [R=301,L]
It redirects just fine. So that makes me think there is an issue with the first part of the rewrite rule, maybe? I'm not too familiar with this sort of stuff.
Since you want to redirect and rewrite the same request, you need to match against %{THE_REQUEST} variable otherwise you will get an infinite loop error on the redirect
RewriteEngine on
# redirect /shows/index.php?id=1 to /shows/1/index.php
RewriteCond %{THE_REQUEST} /shows/index.php\?id=([^\s]+) [NC]
RewriteRule ^shows/index.php$ /shows/%1/index.php? [NC,L,R]
#rewrite /shows/1/index.php to /shows/index.php?id=1
RewriteRule ^shows/([0-9]*)/index.php$ /shows/index.php?id=$1 [L]
Clear your browser cache before testing this.

301 redirect get pagination

I'm trying to 301 redirect a paginated blog list from an old site onto a new url.
I think I'm getting pretty close with the RewriteRule but I'm not quite there yet, this is what I have:
RewriteCond %{QUERY_STRING} ^page=
RewriteRule ^(blog)?$ http://www.newdomain.com/news/page/$1? [R=301,L]
Using this rule if I go to
http://www.olddomain.com/blog?page=1
I currently get redirected to
http://www.newdomain.com/news/page/blog
I would like to be sent to
http://www.newdomain.com/news/page/1
I'm sure its just something small and simple that I'm missing.
Edit
Expanding on the solution below, I've added tags/category support to the rewrite rule using $1.
RewriteCond %{QUERY_STRING} ^page=([^&]+) [NC]
RewriteRule ^blog/tag/([^/\.]+)?$ http://www.newdomain.com/news/tag/$1/page/%1? [R=301,L,NC]
Few minor mistakes in your code.
You need to capture page parameter's value from query string first
Then use that capture value using % instead of $1
No need to capture blog since you don't need it.
Change your code with:
RewriteCond %{QUERY_STRING} ^page=([^&]+) [NC]
RewriteRule ^blog/?$ http://www.newdomain.com/news/page/%1? [R=301,L,NC]

HTaccess dynamic URL to static rewrite that works both ways without loop

I have looked through stack overflow and can't find the exact answer I am looking for so here goes.
I have a URL
http://holtplantandmachinery.co.uk/cat/wheel-loaders which redirects to http://holtplantandmachinery.co.uk/category.php?name=wheel-loaders
However I need it to work the other way around when you enter the dynamic URL it rewrites to the static (folder looking) URL above and vice versa. Essentially whatever one is called it should rewrite to the static one. Now I know I need to setup a rewrite that works the opposite way to the one above and then have a condition to stop an infinite loop but I can't seem to get anything to work please help.
When someone types http://holtplantandmachinery.co.uk/category.php?name=wheel-loaders into the browser's URL address bar, the request sent to the holtplantandmachinery.co.uk server looks something like:
GET /category.php?name=wheel-loaders HTTP/1.1
Host: holtplantandmachinery
some other headers
etc
You can use the %{THE_REQUEST} var to match against the first line:
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ category\.php\?name=([^&\ ]+)
RewriteRule ^ /cat/%2? [L,R=301]
This will redirect the request to http://holtplantandmachinery.co.uk/cat/wheel-loaders and that URL will show up in the browser's URL address bar. The reason why you use %{THE_REQUEST} instead of %{QUERY_STRING} is because when your other rule that rewrites the nicer looking URL to the one with the query string, it'll look and the %{QUERY_STRING} would match, the rule would get applied and cause a redirect loop. Alternatively, you could match against the query string and use a:
RewriteCond %{ENV:REDIRECT_STATUS} !200
To ensure the URI wasn't from a previously rewritten internal redirect.
Then you'd have your internal rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?cat/(.*)$ /category.php?name=$1 [L]

mod_rewrite so that first-level subdirectory is a GET variable

Alright, title is REALLY sloppy.
Here's my problem: I have a news site and when you go to the main page (domain.com) it redirects you to domain.com/news/top?geography=San_Francisco after it figures out your geography.
How do I use the .htaccess so that it goes from domain.com/news/top?geography=San_Francisco domain.com/San_Francisco/news/top ?
There are some similar questions, but I have not found one similar enough in that you're editing the URL as a furtherback subdirectory.
It should also be noted that I am using the Code Igniter framework for PHP and it normally has it as domain.com/index.php/news/top?geography=San_Francisco but I did a mod_rewrite already to get rid of the index.php. The code is as follows for that:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Code I've tried:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Before the index.php rule that you have, try adding this:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
You'll need to make sure the links you generate are in the form of domain.com/San_Francisco/news/top though.
But to take care of the links in the wild that still look like the old way, you have to match against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news/top\?geography=([^&]+)
RewriteRule ^news/top$ /%1/news/top? [L,R=301]
This will 301 redirect the browser if someone goes to the link domain.com/news/top?geography=San_Francisco and make it so the browser's address bar says this: domain.com/San_Francisco/news/top. At which point the browser will send another request for the second URL, and you use the rule above to change it back into the one with a query string.

.htaccess redirect from GET variable to url string

I need to redirect
/search?keywords=somesearchterm
to
/search/somesearchterm
This seems incredibly basic but I've been pounding my head against it for an hour.
Thanks for taking the time to look at this.
You want to implement what is called a "301 Redirect" with mod_rewrite.
RewriteEngine ON
RewriteRule ^/search\?keywords=somesearchterm$ /search/somesearchterm
adding regular expressions:
RewriteEngine ON
RewriteRule ^/search\?keywords=(.+) /search/$1 [R=301,L]
R=301 means provide a 301 Header redirect so the user's URL changes in the browser, and L means don't process any more rewrite rules if this one matches.
If you want to do the reverse -- in other words, if someone goes to mysite.com/search/asearchterm and you want the URL to stay the same, but "behind the scenes" you want it to load a certain server script, do this:
RewriteEngine ON
RewriteRule ^/search/(.+) /search.php\?keywords=$1 [L]
You can not match aginst Query string in RewriteRule directive. Use %{THE_REQUEST} or %{QUERY_STRING} server variables to match the Query string :
The following rule works fine for this redirection
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/search\?kewords=([^&\s]+) [NC]
RewriteRule ^ /search/%1? [NE,NC,R,L]
RewriteRule ^search/([^/]+)/?$ /search?keyword=$1 [QSA,NC,L]

Resources