How to redirect rewritten url - .htaccess

URL is rewritten using following rule.
RewriteEngine on
RewriteRule ^([a-zA-Z]+)/$ category?id=$1
to change
http://localhost/newsite/category?id=home
to following structure
http://localhost/newsite/home/
Now I tried to redirect, newsite/category?id=home to newsite/home/, to make clean URL using redirect rule, such as 301, redirect, but it doesn't work.

You can use this set of rules in newsite/.htaccess:
RewriteEngine on
RewriteBase /newsite/
RewriteCond %{THE_REQUEST} /category(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/$ category.php?id=$1 [L,QSA]

You missed the .php in the rewrite statement
RewriteRule ^([a-zA-Z]+)/$ category?id=$1
change to
RewriteRule ^([a-zA-Z]+)/$ category.php?id=$1

Related

htaccess /index.php?u=xx&p=yy to /xx/yy/ URL rewrite

I would like to know how to URL change /index.php?u=xx&p=yy to /xx/yy/.
index.php will be removed and u and p values become with a slash mark.
htaccess code:
RewriteCond %{THE_REQUEST} /index\.php[\s?/] [NC]
RewriteRule ^(.*?)index\.php(/.*)?/?$ /$1$2 [L,R=301,NC,NE]
I have used the above code but it only removes index.php. Others are remaining the same.
With your shown samples/attempts, please try following htaccess rules. Please make sure to clear your browser cache before testing your URLs.
Also please make sure that your htaccess rules file and index.php files are in same directory.
RewriteEngine ON
##External redirect rules as follows:
RewriteCond %{THE_REQUEST} \s/p/index\.php\?u=([^&]*)&p=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Internal rewrite rules as follows:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ index.php?u=$1&p=$2 [QSA,L]

Redirect urls with question mark and other parameters in htaccess

I want to redirect pages like:
/category-name/post-name.html?id=1234
To:
/category-name/1234-post-name.html
How can do this using htaccess?
What I have tried:
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^([^/]+)/([^.]+)\.html$ /$1/%1-$2\.html [L,R=301]
But it is a continuous redirect.
You can use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+([\w-]+)/([\w-]+)\?id=([^\s&]+) [NC]
RewriteRule ^ /%1/%3-%2? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^([\w-]+)/([^-]+)-([\w-]+)/?$ $1/$3?id=$2 [L,QSA]
Try this
Make sure the url is root url
Example:-
www.foo.com/category-name/post-name.html
It will only work if the project url is same as that of the above.
www.foo.com/blog/category-name/post-name.html
This won't work you need to update the RewriteBase url accordingly l.
This is the conditions
RewriteEngine On
RewriteBase /
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule here
RewriteRule ^category-name/([0-9]+)-post-name$ /category-name/post-name.html?id=$1 [L]
This should work..

Htaccess possible 301 redirect

I have a question about htaccess.
I have a url like this :
http://www.asd.com/producten/kia?category=car
And I want this to be :
http://www.asd.com/producten/car/kia
Is that possible with htaccess?
I have tried different ways to solve it but so far no luck.
I've tried:
RewriteRule ^producten/([^/]+)/([^/]+) /producten/$2?category=$1 [NC]
AND
RewriteRule /producten/(.*)/(.*) /producten/$2?category=$1 [R]
Thanks in advance,
Mert
Try using the following in your /.htaccess file:
RewriteEngine On
# Step 1: Redirect the old URI to the new one and prevent looping
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} category=([^\s&]+) [NC]
RewriteRule ^(producten)/([^/]+)/? /$1/%1/$2? [R=302,L,NE]
# Step 2: Internally rewrite the new URI to the old one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(producten)/([^/]+)/([^/]+)/?$ /$1/$3?category=$2 [L,QSA]
If you would like to make the redirect permanent, change R=302 to R=301.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /(producten)/([^?]+)\?category=([^\s&]+) [NC]
RewriteRule ^ /%1/%3/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^(producten)/([^/]+)/([^/]+)/?$ $1/$3?category=$2 [L,QSA,NC]

.htaccess RewriteRule not overriding URL

I have this .htaccess which basically is supposed to turn my links from
http://www.dsbigiena.ro/products.php?product-id=Colac-wc-cu-folie-de-plastic-cu-actionare-manuala-prin-buton
to
http://www.dsbigiena.ro/Colac-wc-cu-folie-de-plastic-cu-actionare-manuala-prin-buton/
Unfortunately, it's not doing it.
Did I write it wrong?
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]*)/$ /products.php?product-id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
EDIT: I did indeed test the .htaccess with "deny from all", and it works.
Your current rewrite rule only resolve a pretty URL. It won't convert an actual /products.php URL to the pretty one by itself. You need to have an explicit rule for that as
RewriteEngine On
RewriteBase /
# Redirects from a normal URL to a pretty one
RewriteCond %{THE_REQUEST} \ /products\.php\?product-id=([^\ ]+) [NC]
RewriteRule ^ /%1/? [L,R=301]
# Resolves the pretty URL
RewriteRule ^([^/]+)/?$ /products.php?product-id=$1 [QSA,L]

Help with mod_rewrite and mod_redirect

My .htaccess file is:
Redirect 301 http://domain.com/news/articles?dtMain_start=150 http://domain.com/news/articles
Redirect 301 http://domain.com/news/articles?dtMain_start=160 http://domain.com/news/articles
Redirect 301 http://domain.com/news/articles?dtMain_start=170 http://domain.com/news/articles
#
RewriteEngine On
RewriteBase /
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I also have to incorporate the following rule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.co.uk/$1 [R=301,L]
I cannot get them to work together... can anyone help...
I tried just stacking the Redirects before the RewriteCond and I get this...
http://www.domain.com/news/articles?q=news/articles?dbMain_start=150
ie http://domain.com/newpage?q=oldpage
Okay Mod_Alias and Mod_Rewrite don't like each other.
Can I write something like:
RewriteCond %{REQUEST_QUERY_STRING} ^.*&bodgeredirect=true$
RewriteRule ^(.*)&bodgeredirect=true$ index.php?q=$1 [L,QSA]
First of all: There is not mod_redirect. Redirect is a directive of mod_alias.
And the Redirect directive, like any other directive of mod_alias, does only work with the URL path. So your Redirect directives won’t work as expected. Use mod_rewrite equivalents instead:
RewriteCond %{HTTP_HOST} =example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^dtMain_start=(150|160|170)$
RewriteRule ^news/articles$ /news/articles? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
In general it is not a good idea to mix mod_alias and mod_rewrite if the patterns coincide with each other.

Resources