.htaccess rewriterule swap around url - regex

Trying to htaccess rewrite
old-url-part/qwerty/999-yuiop ...to...
new-url-part/yuiop-999/qwerty
RewriteRule ^old-url-part/(.*)/([0-9]+)-(.*)/?$ /new-url-part/$3-$2/$1/ [R=301,L]
The following part works fine, but struggling with the above line
RewriteRule ^old-url-part/?$ /new-url-part/ [R=301,L]

This the rule you need:
RewriteRule ^old-url-part/([^/]+)/([^/]+)/?$ new-url-part/$2/$1/ [R=301,L,NC]

Related

url rewrite rule for paramters

I have the following URL:
https://www.mydomain.de/termin_cancel=c502b486-e8ad-490a-a615-80307a515fc8
Original url:
https://www.mydomain.de/index.php?termin_cancel=c502b486-e8ad-490a-a615-80307a515fc8
rewrite rule:
RewriteRule ^termin_cancel=([\w-]+)/?$ index.php?termin_cancel=$1 [L,NC,QSA]
This works !
But now I have a problem with the next situation:
The url should be:
https://www.mydomain.de/termin_change=XXX&serviceID=XXX&firstname=XXX&lastname=XXX&email=XXX
original url:
https://www.mydomain.de/index.php?termin_change=XXX&serviceID=XXX&firstname=XXX&lastname=XXX&email=XXX
I don't know which htaccess rule work fo this.
It should be possible to access the parameters via php $_GET.
This should work for you.
RewriteRule ^termin_change=([^&]+)&serviceID=([^&]+)&firstname=([^&]+)&lastname=([^&]+)&email=([^&]+)$ /index.php?termin_change=$1&serviceID=$2&firstname=$3&lastname=$4&email=$5 [L,NC]
Could you please try following.
RewriteEngine ON
RewriteRule ^(.*) index.php?$1 [QSA,NE,L]
OR with / in case your index.php is present in root directory/folder.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*) /index.php?$1 [QSA,NE,L]
OR(either put above or put following)
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} ^/termin_change=.*/?$ [NC]
RewriteRule ^(.*) /index.php?$1 [QSA,NE,L]
Try, something like below specifically for termin_change=
RewriteRule ^termin_change=(.+)/?$ index.php?termin_change=$1 [L,NC,QSA]

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]

Multiple domains in one .htaccess

I have two domains linked in one FTP, and for me need rewrite .htaccess file, for work like that:
site.lt (redirect)-> site.lt
site.lt/lv (redirect)-> site.lt
site.eu (force redirect)-> site.eu/lv
now I have wrote this code in my .htaccess:
RewriteCond %{HTTP_HOST} ^site\.(lt|eu)
RewriteRule ^(.*)$ http://site.(lt|eu)/$1 [R=permanent,L]
RewriteRule ^$ http://site.eu/lv [R=301,L]
and it works like:
site.lt (redirect)-> site.eu/lv
site.lt/lv (redirect)-> not working
site.eu (redirect)-> site.eu/lv
how to rewrite htaccess to make it work properly?
You can use these rules:
RewriteCond %{HTTP_HOST} ^site\.lt$
RewriteRule ^(lv)?/?$ / [R=301,L,NC]
RewriteCond %{HTTP_HOST} ^site\.eu$
RewriteRule ^$ /lv [R=301,L]

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]

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