Redirection loop (URL rewriting) - regex

I'm trying to rewrite my website URLs. I have:
http:// website.com/v2/message.php?ID2=123
I want:
http:// website.com/v2/message-123.php
I tried something in my htaccess but I've a redirection loop :'(
Here is my .htaccess:
RewriteCond %{QUERY_STRING} ^ID2=([0-9]+)$
RewriteRule message.php? http://website.com/v2/message-%1.php? [L,R=301]
Can someone help me with this?

I suggest not using .php in pretty URL, make it extension-less. Try these rules in v2/.htaccess:
RewriteEngine On
RewriteBase /v2/
RewriteCond %{THE_REQUEST} /message\.php\?ID2=([^&\s]+) [NC]
RewriteRule ^ message-%1? [NE,R=302,L]
RewriteRule ^message-(.+)/?$ message.php?ID2=$1 [L,QSA,NC]

Related

I'm having an issue with my .htaccess redirection

I need to use this url http://yoursite.com/proprietes.php?viewid&mls=[18348939] but I wan it to go on a pdf a-very-new-post.pdf. I want to do a redirection in the htaccess. It is not working... I'm in wordpress
thanks!
RewriteEngine On
Redirect 301 /proprietes.php?viewid&mls=[18348939]$ http://yoursite.com/a-very-new-post.pdf
Redirect or RewriteRule directives cannot match query string. Use a RewriteCond instead like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/proprietes\.php\?viewid&mls=\[?18348939\]? [NC]
RewriteRule ^ /a-very-new-post.pdf? [L,B,R=301]
RewriteCond %{THE_REQUEST} \s/en/proprietes\.php\?viewid&mls=\[?18348939\]? [NC]
RewriteRule ^ /a-very-new-post-english.pdf? [L,B,R=301]

Redirect 301 with params

I am redirecting traffic between old web and new web and I need to redirect 301 from /hotel.php?hotel=lasvegas&lng=es to http://www.new-web.com/hotel-las-vegas/
How could be possible from htaccess?
I have tried
Rewriterule ^hotel\.php\?hotel=lasvegas\&lng=es http://www.new-web.com/hotel-las-vegas/ [L,R=301]
I think my error is at regular expression. Can anyone help me?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /hotel\.php\?hotel=lasvegas&lng=es [NC]
RewriteRule ^ hotel-las-begas? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^hotel-las-vegas/?$ hotel.php?hotel=lasvegas&lng=es [L,QSA,NC]
it's very important that you insert best code of redirect 301 in best place of htaccess. for example :
http://www.abartazeha.com/
we replace abartazeha.com instead of abartazeha.ir
...
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]

How do I handle this htaccess rewrite?

I have a url that is giving me trouble in terms of a rewrite:
http://www.example.com/my-directory/?image=21
I have tried this:
Redirect 301 /http://www.example.com/my-directory/?image=21 http://www.example.com
Or I know that something along these lines is probably on the right path but I dont think I have the rewrite condition quite right:
RewriteCond %{QUERY_STRING} ^(?image=21)$ [NC]
RewriteRule ^/?$ /? [R=301,L]
Try this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^image=21$ [NC]
RewriteRule ^my-directory/?$ http://www.example.com/? [R=301,L]

Apache Rewrite Issue. Unable to figure out query string

So I have been struggling on finding the rule to match this rewrite. I am working on a client website and it is a nightmare with the number of duplicate title tags. I have managed to resolve most of them by enforcing forward slash, redirect non www. to the www. version and disallow crawling of https version of the website.
The issue I am having at the moment. I have over 1000 URLs that are duplicate content, each product has two different URLs with the exact same content. An example is:
http://www.example.co.uk/product/widget1/
http://www.example.co.uk/widget1/
http://www.example.co.uk/product/widget2/
http://www.example.co.uk/widget2/
Now the following URLs have the same content:
http://www.example.co.uk/product/widget1/
http://www.example.co.uk/widget1/
I want to redirect any URL that contains "/product/" to the URL version without "/product/" in the URL if that makes sense. I honestly don't know where to start and would really appreciate the help.
Thanks in advance
EDIT: The recommended rule:
RewriteEngine On
RewriteRule ^/product/(.*)$ /$1 [R=301]
does not work. It may be conflicting. These are the other rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteRule ^/product/(.*)$ /$1 [R=301]
RewriteCond %{HTTP_HOST} ^example\.co [NC]
RewriteRule (.*) http://www.example.co.uk/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
I dont know if there are any conflicts here. Please help
Have your full .htaccess like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.co [NC]
RewriteRule (.*) http://www.example.co.uk/$1? [L,R=301]
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?[^/])$ /$1/ [L,R=301]
RewriteRule ^product/([^/]+)/?$ /$1/ [R=301,L]
Assuming the URLs always start with product, this should work:
RewriteEngine On
RewriteRule ^/product/(.*)$ /$1 [R=301]
It'll need to go in your main site conf or .htaccess

How to handle both /url and /url/ redirects with htaccess?

I am rewriting all URLs and shorthand some of them:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(([^\.]+))\.pl [NC]
RewriteRule ^(.*)$ http://%1.pl%{REQUEST_URI} [R=301,L]
RewriteRule ^test$ test-page.php [R=301,L]
so that
example.pl/test redirects to example.pl/test-page.php
but
example.pl/test/ redirects to example.pl/
while it should:
example.pl/test/ redirects to example.pl/test-page.php
How to handle both /url and /url/ redirects?
I assume that it has to something with trailing slash.
You need to make trailing slash optional using /?$ regex. Try these rules:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.pl [NC]
RewriteRule ^ http://%1.pl%{REQUEST_URI} [R=301,L]
RewriteRule ^test/?$ test-page.php [NC,R=301,L]
Make sure to test this in a new browser to avoid 301 caching issues.
Try this:
RewriteEngine On
RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1
RewriteRule ^test$ test-page.php [R=301,L]
Tested here.