How do I handle this htaccess rewrite? - regex

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]

Related

.htaccess rewrite w/ regular expression and ?lang=en parameter

I need a hand with creating a regex rewrite rule for my .htaccess.
Here's the problem. My previous URLs structure was:
http://example.com/whatever-the-url-is/?lang=en
now I turned it into
http://example.com/en/whatever-the-url-is/
I'd like to create an .htaccess URL that 301 redirects all the URLs from the previous structure to the new one.
Also the .htaccess is shared between different domains, and so there should be
RewriteCond %{http_host} !^example.com$ [NC] condition at the beginning...
Is it possible? Do you have any suggestions?
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteCond %{QUERY_STRING} (?:^|&)lang=(.+?)(?:&|$) [NC]
RewriteRule ^(.*)$ /%1/$1? [R=301,L]
You can add the RewriteRule after RewriteCond %{HTTP_HOST}:
RewriteCond %{REQUEST_URI} !^/(?:en|fr|sp)/ [NC]
Where you test if langcode is already in the url.

How to get first subdomain with RewriteCond htaccess?

Consider this URL:
www.sub1.sub2.sub3.subdomain.domain.com
The only thing that is a given is "domain.com". Now, I want to forward this UGLY url to: subdomain.domain.com. So somehow I have to get the subdomain.
I tried:
RewriteCond %{HTTP_HOST} ^(.+)/.([^.]+)/.domain.com$ [NC]
RewriteRule ^(.*)$ http://%2.domain.com/$1 [L,R=301]
Which won't work, as htaccess doesn't support look behind.
Any workaround for this problem?
You can use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.+?\.([^.]+\.domain\.com)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

Redirection loop (URL rewriting)

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]

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]

Rewriterule with redirect 301 and regexp not working

I have got few issues with redirect 301 and rewriterule in htaccess.
I've got few thousands subpages with new links, which have to be redirected permamently, but I can manage it with 10-20 rewriterules. But not everything is going as it should be...
Basic rewriterule works just fine, example:
RewriteRule ^subpage-([0-9]+)*\.html$ subpage.php?p=$1 [L]
It gets me from subpage-1.html to subpage.php?p=1 (no 301 needed here, it's just example, rewriterule does it's job).
Simple rewriterule with 301 redirect also works fine:
RewriteRule ^subfolder/subpage.php$ /subpage.html [L,R=301]
Althrough I don't know why I have to put "/" before "new-subpage". If I don't I'm being redirected to "domain/whole-ftp-path/new-subpage.html" and not "domain/new-subpage.html". Is it just redirect 301 thing?
And the main event:
RewriteRule ^subfolder/subpage.php?p=([0-9]+)$ /subpage-$1.html [L,R=301]
This does nothing, I'm getting domain/subfolder/subpage.php?p=1 with 404 (old subpages does not exist anymore in the same location).
What I am doing wrong?
You cannot match QUERY_STRING in RewriteRule.
Replace your .htaccess with this:
RewriteCond %{THE_REQUEST} \s/+subfolder/oldsubpage\.php\?f=([^\s&]+) [NC]
RewriteRule ^ /newsubpage-%1.html? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+subfolder/oldsubpage\.php[\s/] [NC]
RewriteRule ^ /newsubpage.html? [R=302,L]
RewriteRule ^subpage-([0-9]+)*\.html$ /subpage.php?p=$1 [L,NC,QSA]
RewriteRule ^newsubpag\.html$ /newsubpage.php [L,NC]
Has to do with the query string. If you have only one subfolder you could try this:
RewriteCond %{QUERY_STRING} p=([0-9]+)
RewriteRule .*$ /subpage-%1.html [L,R=301]
With more subfolders you could try:
RewriteRule ^subfolder/subpage.php(.*)$ /subpage$1 [QSA]
RewriteCond %{QUERY_STRING} p=([0-9]+)
RewriteRule ^(.*)$ /$1-%1.html [L,R=301]