.htaccess redirect, change GET variables - .htaccess

Very simple question. I need to change url like
http://www.mysite.com/index.php?page=[x]
where [x] is any number to
http://www.mysite.com/index.php?id=[x]
Just to change ?page= to ?id= using .htaccess 301 redirect.
How can I do this?

Match the QUERY_STRING inside a RewriteCond:
RewriteEngine On
# Capture (\d+) into %1
RewriteCond %{QUERY_STRING} page=(\d+) [NC]
# And rewrite (redirect) into id=%1
RewriteRule ^index\.php$ /index.php?id=%1 [L,R=301]
The above would only rewrite requests to index.php. If you want to rewrite everything, instead use
RewriteRule ^(.*)$ /$1?id=%1 [L,R=301]

Related

Continuous redirections in .htacess

How to get rid of the error that causes continuous redirection? I would like these rules to work in both directions.
RewriteEngine On
RewriteRule ^search/([a-z0-9-]+)/$ search.php?name=$1
RewriteCond %{QUERY_STRING} ^name=(.*)$
RewriteRule ^search\.php$ https://example.com/search/%1/? [L,R=301]
there's a redirection loop
you may use an environnement variable to check if your url is in a Redirection context
# redirect pretty url to real php
RewriteRule ^search/([a-z0-9-]+)/$ search.php?name=$1 [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^name=(.*)$
# force pretty url with 301 unless in a rewrite
RewriteRule ^search\.php$ https://example.com/search/%1/? [L,R=301]

mod_rewrite: url rewrite and redirect

I have the following URL:
http://example.com/pages/cms/impressum.php
and want to get an URL like this:
http://example.com/impressum
My rewrite Rule is:
RewriteEngine on
RewriteBase /
RewriteRule ^impressum$ /pages/cms/impressum.php [R,L]
The Problem is, I can open the url in both ways. I would like a forwarding from /pages/cms/impressum.php to /impressum. If I use [R=301], the second URL which I do not want works. I want to reverse this rule.
Use:
RewriteEngine On
RewriteBase /
# To externally redirect pages/cms/impressum.php to impressum
RewriteCond %{THE_REQUEST} \s/+pages/cms/impressum\.php[\s?] [NC]
RewriteRule ^ /impressum [R=301,L]
# To internally forward impressum to pages/cms/impressum.php
RewriteRule ^impressum/?$ pages/cms/impressum.php [L,NC]

php - Htaccess as fake directories

I am making a mini blog that could make it's url looks like this:
From: http://127.0.0.1/index.php?post=the-story-of-us
To: http://127.0.0.1/view/the-story-of-us
I have tried this but i'm getting 404 not found.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?post=([^&]+)
RewriteRule ^ /view/%2/? [L,R=301]
Your current rule only handles the case: Redirect old url to new url.
(By the way, +1 for using THE_REQUEST to avoid a redirect loop)
You also need to handle the case: Rewrite (internally) new url to old url.
Here is how your htaccess should look like
RewriteEngine On
# Redirect /index.php?post=XXX to /view/XXX
RewriteCond %{THE_REQUEST} \s/index\.php\?post=([^&\s]+)\s [NC]
RewriteRule ^ /view/%1? [L,R=301]
# Internally rewrite back /view/XXX to /index.php?post=XXX
RewriteRule ^view/([^/]+)$ /index.php?post=$1 [L]
I do not udnerstand your RewriteCondition, but the RewriteRule should look like this:
RewriteEngine on
RewriteBase /
RewriteRule ^view/(.*)/? ./index.php?post=$1 [L,R=301]

htaccess weird revert happening with a url rewrite rule and a 301 redirect

I'm having issues trying to do a 301 redirect in my htaccess file. It is having a problem working with a part of my rewrite rules I have created. In summary, the rewrite strips the extension off of the page and replaces the query ?primary-view=city to /city. Here is my htaccess file.
RewriteEngine On
# Redirect /page.php to /page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(page)\.php [NC]
RewriteRule ^ /%1 [R=302,L]
# Internally forward /page to /page.php
RewriteRule ^(page)/?$ /$1.php [L,QSA,NC]
# Redirect /page.php?primary-view=value to /page/value
RewriteCond %{THE_REQUEST} \s/+(page)\.php\?primary-view=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
# Internally forward /page/value to /page.php?primary-view=value
RewriteRule ^(page)/(.*)/?$ /$1.php?primary-view=$2 [L,QSA,NC]
# Redirect /chicago-downtown to /chicago
Redirect 301 /chicago-downtown to /chicago
So the url after the rewrite will look something like
#Original url
example.com/page.php?primary-view=chicago-downtown
#Url rewritten properly
example.com/page/chicago
If I type in example.com/page/chicago then it works fine. If I type in example.com/page/chicago-downtown its currently redirecting to the following:
#Current bad redirect from example.com/page/chicago-downtown
example.com/page/chicago?primary-view=chicago-downtown
#Desired redirect from example.com/page/chicago-downtown
example.com/page/chicago
I have tried man things and came to the conclusion that it is the following line causing the issue with the 301 redirect.
# Internally forward /page/value to /page.php?primary-view=value
RewriteRule ^(page)/(.*)/?$ /$1.php?primary-view=$2 [L,QSA,NC]
Is there some way I can rewrite my rewrite rules, or is there a rewrite condition I need to use to prevent this from happening?
You don't need Redirect rule. Just an additional mod_rewrite rule for chicago-downtown => chicago:
RewriteEngine On
RewriteRule ^(page/chicago)-downtown/?$ /$1 [L,R=302,NC]
# Redirect /page.php to /page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(page)\.php [NC]
RewriteRule ^ /%1 [R=302,L]
# Internally forward /page to /page.php
RewriteRule ^(page)/?$ /$1.php [L,QSA,NC]
# Redirect /page.php?primary-view=value to /page/value
RewriteCond %{THE_REQUEST} \s/+(page)\.php\?primary-view=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
# Internally forward /page/value to /page.php?primary-view=value
RewriteRule ^(page)/(.*)/?$ /$1.php?primary-view=$2 [L,QSA,NC]
This is probably caused because you're mixing mod_alias directives with mod_rewrite directives. That means both directives get applied to the same request in an unwanted way. Remove the Redirect directive (mod_alias) and user a rewrite rule instead. But you need to add the rule before your other rules (i.e. right after RewriteEngine On)
RewriteRule ^chicago-downtown(.*)$ /chicago$1 [L,R=301]

Checking empty request uri with .htaccess

I need to redirect the request from
www.mysite.com/?querystring=data
to
www.mysite.com/dir/phpfile.php/?querystring=data
It means that it should be translated only the url with empty request_uri ( for example
www.mysite.com/css/style.css
should not be translated), and with not empty query string ( for example the main page
www.mysite.com/
should not be translated).
I wrote this code
RewriteCond %{HTTP_HOST} ^www.mysite.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^/?(.*)$ www.mysite.com/$1 [QSA]
But it doesn't work. Any suggestion?
The rule that you have has the domain name in it without a protocol, it's going to look like a URI or file pathname to mod_rewrite. Additionally, you're rewriting it nothing, since $1 backreferences the grouping (.*) in your match, which must be nothing since your condition says the URI can only be /. You probably want something like:
RewriteCond %{HTTP_HOST} ^www.mysite.com$ [NC]
RewriteRule ^/?$ /dir/phpfile.php/ [L,R]
The query string will automatically get appended to the end. Remove the R if you don't want to externally redirect the browser (thus changing the URL in the location bar), or replace it with R=301 if you want a permanent redirect.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.+)$ [NC]
RewriteRule ^$ /dir/phpfile.php/ [L,QSA,R=302]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Resources