Htaccess possible 301 redirect - .htaccess

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]

Related

Clean URLs in a subfolder

I've read and followed guides and looked for answers to other people's questions, but I'm struggling with url rewriting and htaccess.
I have a subfolder on my site with an index page which handles query strings to bring up dynamic content. I'd like the variable to appear to be a subfolder name e.g.
http://mywebsite.com/subfolder/index.php?v=variable
to
http://mywebsite.com/subfolder/variable
The .htaccess file I've made is at http://mywebsite.com/subfolder/ i.e. the same folder as index.php
How can I get this to work? Any help gratefully appreciated.
You can use these rules inside your /subfolder/.htaccess
RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index.php?v=$1 [L]
Update (passing two values)
RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)&v2=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)\s [NC]
RewriteRule ^ %1? [R=301,L]
# Don't touch to existing files/folders
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite /xxx to /index.php?v=xxx
RewriteRule ^([^/]+)$ index.php?v=$1 [L]
# Rewrite /xxx/yyy to /index.php?v=xxx&v2=yyy
RewriteRule ^([^/]+)/([^/]+)$ index.php?v=$1&v2=$2 [L]
Use this in your .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} v=(.+)$ [NC]
RewriteRule ^ subfolder/%1? [R=301,L,NE]
This grabs the variable using %{QUERY_STRING} and then appends it to the rewrite using %1. You'll see I've added a ? onto the end of the rewrite. That is to stop the original query from appearing on the end of the URL.
I've used R=301 which is a permanent redirect. You might want to changes this to R=302 while you're testing, as this is temporary.
You can view a test of this rule working here: https://htaccess.madewithlove.be?share=7b6832e9-2c05-5d1d-916c-e4dd0f5b1da6
Make sure you clear your cache before testing this.

How to redirect rewritten url

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

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..

redirect all urls with "videos.php" to base

I'm hoping someone can assist me with a mod_rewrite rule to redirect dynamic urls pointing to "videos.php" on my server to the base url.
For example, I need to redirect 'website.com/1/music/various/videos.php?=1234'
to 'website.com/videos.php?=1234'
edit: I am looking for a dynamic solution. If a url is pointed to videos.php at any time, I need to do a 301 redirect to home directory. Ie if /1/home/music/videos.php?=1234 redirect to /videos.php?=1234, or /music/playlist/1234/videos.php?1432 to /videos.php?1432.
Create a .htaccess file and insert these lines:
RewriteEngine On
# Page moved permanently:
RewriteRule ^videos\.php\?\=([0-9]+)\.html$ /1/music/various/videos.php?=$1 [R=301,L]
When testing, leave out the R=301, part, or you'll only see cache contents for a long, long time. Add it when sure its working fine. Or use 302, which means temporarily.
Please check this:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/1/music/various/videos.php$
RewriteCond %{QUERY_STRING} (.+)$
RewriteRule ^(.*) /videos.php [R,QSA]
RewriteCond %{REQUEST_URI} ^/videos.php$
RewriteCond %{QUERY_STRING} !(.+)$
RewriteRule ^(.*) http://%{HTTP_HOST}/ [R=301]
If it's not work, then try this:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/1/music/various/videos.php$
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^(.*) /videos.php%1 [R]
RewriteCond %{REQUEST_URI} ^/videos.php$
RewriteCond %{QUERY_STRING} !(.+)$
RewriteRule ^(.*) http://%{HTTP_HOST}/ [R=301]

.htaccess mod_rewrite cannot remove page name

Having trouble figuring out the mod rewrite for .htaccess I want the url http://www.example.com/archive.php?title=about_me which is a dynamic url to be rewritten to http://www.example.com/about_me. I am using php and here is my current .htaccess code, however it only rewrites to http://www.example.com/archive/about_me want the archive to be removed.
Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /archive/%1? [L,R=301]
RewriteRule ^/?archive/(.*)$ /archive?title=$1 [L]
## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
I did get it to rewrite correctly with this code
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
RewriteRule ^/(.*)$ /archive?title=$1 [L]
However it then returns a page cannot be found error
I you want the /archive/ to be removed, you'll have to ensure that any URI that's in the form of /something must absolutely be routed to the archive.php script. Because there's simply no way to tell whether /my_blog is actually a request for /my_blog or whether it needs to be sent to the archive.php script with "my_blog" as the value of title in the query string. The best you can do is check that it's not a request for an existing resource via the -f and -d conditions:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
# no /archive/ ^
# condition checks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /archive?title=$1 [L]
Something like this should do the trick:
RewriteCond %{QUERY_STRING} ^title=(.*)
RewriteRule ^archive.php /%1?
RewriteRule ^(.*)$ /archive?title=$1 [L]
EDIT: Added the final RewriteRule as noted in my comments on the original question. Per the comment I believe you are trying to do the following two things:
Redirect any user-entered "real" URLs to the "friendly" URL: http://www.example.com/archive.php?title=about_me to http://www.example.com/about_me as stated in the question.
Rewrite the "friendly" URL to the "real" URL: http://www.example.com/about_me to http://www.example.com/archive.php?title=about_me, which was not clear as stated.

Resources