htaccess - rewrite rule - redirect after url rewrite - .htaccess

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.

Related

Problem with using Rewriterule in htaccess

With RewriteRule I've always cleaned my URLs as following:
RewriteRule ^page/(.*)$ page.php?urlkey=$1 [QSA]
My new host doesn't allow me to use Options +FollowSymLinks and therefore I cannot use the / anymore. So I've changed my RewriteRule to:
RewriteRule ^page-(.*)$ page.php?urlkey=$1 [QSA]
However, I need to redirect all my former URLs to the new version. I tried doing this using the following rule:
RewriteRule ^page/(.*)$ page-(.*)$ [R=301,L]
This is however not working. I've also tried to just make a Redirect in my .htaccess:
Redirect 301 https://www.example.com/page/urlkey https://www.example.com/page-urlkey
This is also not working.
EDIT
As requested the actual code below:
RewriteEngine on
RewriteRule ^citywalk-(.*)$ citywalk.php?urlkey=$1 [QSA]
RewriteRule ^citywalk/(.*)$ citywalk-(.*)$ [R=301,L]
For example the citywalk The Historical Centre has a urlkey the-historical-centre. The old url is citywalk/the-historical-centre.
To test this specific case and other technique:
Redirect 301 /citywalk/the-historical-centre https://example.com/citywalk-the-historical-centre
By visiting https://example.com/citywalk/the-historical-centre no redirecting takes place (the url stays the same in the browser) and no urlkey is found.

htacess redirect from php page to a different website?

This seems so trivial but I can't seem to get it to work. I'm trying to redirect
http://website.com/something/file.php?var=1
to
http://website2.com/something/$1/
I've tried a bunch of things the latest was
Redirect 301 ^file.php\?var=([0-9]+)$ http://website2.com/something/$1/
I've even tried doing
Redirect 301 ^something/$ http://website2.com/something/1/
but that didn't work either. The only thing that I've gotten to work is a redirect on the whole site doing
Redirect 301 /$ http://website2.com/something/1/
Is there something I'm missing? I've done a lot of redirects in my day but this one is throwing me for a loop. I've even used htaccess checker that said my url matches my string but it didn't actually do anything.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /something/file\.php\?var=(\d+) [NC]
RewriteRule ^ something/%1? [R=302,L,NE]
RewriteRule ^something/(\d+)/?$ something/file.php?q=$1 [L,QSA,NC]

redirect old query URL to new rewritten URL .htaccess

I've successfully made my urls clean using parameters
from example.com/index.php?gender=m&height=70&weight=150
into example.com/m/70/150/
so if someone types in that clean URL, they'll get the page with the proper parameters.
However, I want to make it so if someone types in the old url, it will 301 redirect to the new clean URL
I've tried this
RewriteCond %{QUERY_STRING} ^gender=([a-z]+)&height=([0-9-]+)&weight=([0-9-]+)$
RewriteRule ^index\.php$ http://www.example.com/%1/%2/%3? [R=301,L]
but it is not working, no error, no rewrite.. the URL just looks like the old one still.
Edit:
Current rewrite to make URL's look clean
RewriteEngine on
RewriteRule ^([m]+)en/([0-9-]+)-inches/([0-9-]+)-lbs/?$ index.php?gender=$1&height=$2&weight=$3 [NC,L]
You should probably be checking against the actual request as opposed to the URI/query-string because both of those get modified by other rules. There's also something you're missing in the redirect, the en, -inches, and -lbs in part of the cleaner looking URI.
Try:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?gender=([a-z]+)&height=([0-9-]+)&weight=([0-9-]+)
RewriteRule ^index\.php$ /%1en/%2-inches/%3-lbs/? [L,R=301]

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