How to fix following .htaccess 301 redirect - .htaccess

I know there are a lot of similar questions, but couldn't find a value in them.
I am doing the following redirect in .htaccess
RewriteCond %{REQUEST_URI} ^/old/url/(.*)$
RewriteRule ^ /new-url/$1 [L,R=301]
The final result I would like to get is http://old/url/page/2 --> http://new-url/page/2
but I am getting getting wrong urls, like http://new-url/old/url/page/2
could someone give me a hint how to include only /page/2/ part appended in the final url redirect
thanks

Try:
RewriteCond %{REQUEST_FILENAME} ^/old/url/(.*)$
RewriteRule ^ /new-url/$1 [L,R=301]

Related

301 redirect with GET variables

Good morning all,
When I do a search for the name of my site on google I end up with lots of links like mysite.com/?page=1
mysite.com/?page=2
Etc.
I would like to redirect 301 of these links which ends in mysite.com/?page=X
to monsite.com
Because I am afraid that Google will see it as duplicate content knowing that it displays all the home page of my site ...
I tried
RewriteCond %{QUERY_STRING} ^page=1(&|$) [NC]
RewriteRule ^(mysite)/?$ /$1? [R=301,L]
which doesn't work on my side.
Could you help me ?
Thanks in advance,
To redirect such requests this should be what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)page=\d+(?:&|$) [NC]
RewriteRule ^/?$ / [QSD,R=301,END]
Or a more general example which preserves given a path
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)page=\d+(?:&|$) [NC]
RewriteRule ^ %{REQUEST_URI} [QSD,R=301,END]
Keep in mind however that even with such redirection you still have the issue that somewhere those references are generated. Google does not make them up. So to fix the actual issue and not just a symptom you will have to find the actual issue...

Redirect htaccess stuggles

I would like to make a rewrite rule for my website but I cannot seem to get the proper code in order to make this work.
The current URL of my website is looking like http://www.mohanadarafe.io/JSON/json.html
I want it to be: http://www.mohanadarafe.io/json
I have tried the following code but it does not seem to work:
RewriteEngine On
RewriteRule ^\.html$ /json [L]
Any idea how to fix this?
You have it reversed. Have it like this:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /JSON/json\.html [NC]
RewriteRule ^ /json [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^json/?$ JSON/json.html [L,NC]
Then you can have your URL as: http://www.mohanadarafe.io/json

.htaccess rewrite rule with get params

I need to be able to rewrite http://example.com/staging/details/?postid=23 so that it becomes http://example.com/staging/grandhotelterduin
I am not sure how the .htaccess rule should be?
I have came up with something like this
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^grandhotelterduin/(.*)$ ./details/?postid=23 [L,NC]
I am not sure I am doing this correctly, can you plz help
I want it such that the http://example.com/staging/grandhotelterduin/ get redirected to http://example.com/staging/details/?postid=23 but the url that appears in the browser is `http://example.com/staging/grandhotelterduin
Use this:
RewriteEngine On
RewriteRule ^grandhotelterduin$ /staging/details/?postid=23 [L]
It will give you the following URL:
http://sheetz.nl/grandhotelterduin
EDIT:
RewriteCond %{THE_REQUEST} /staging/details/?postid=([0-9]+) [NC]
RewriteRule ^ /staging/grandhotelterduin [L,R]

htaccess rewrite part of url

Been trying to figure this one out - also found similar examples here on stack overflow, but can't get it to work.
I'm trying to rewrite a URL from
http://www.domain.com/index.php?option=com_users&view=login&return=aHR0cDovL3d3dy5vc
to:
http://www.domain.com/login&return=aHR0cDovL3d3dy5vc
I've tried this:
RewriteRule ^/?index.php?option=com_users&view=login&return(.*)$ /login?return$1 [R=301,L]
But as I said, can't get it to work, so appreciate any help.
Thanks!
You can use this rule as first rule in your .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?option=com_users&view=([^&\s]+)&return=([^&\s]+)
RewriteRule ^ /%1?return=%2 [R=301,L]
You cannot match QUERY_STRING using RewriteRule.

.htaccess code for dynamic redirect

I've having a bit of trouble figuring out how to mass redirect a lot of files.
http://www.mysite.com/verify.php?site=mydomain.com
to
http://www.mysite.com/verify/mysite.com
And of course "mysite.com" will always be a different domain so that should be dynamic.
This is the code that I was using:
RedirectMatch 301 ^/verify\.php\?site=([a-zA-Z0-9\.\-]+)$ /verify/$1
Can someone please post what I need to change to make this work or a correct version of my code above? Thanks for your time!
Try this:
EDIT:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^verify/(.*)$ verify.php?site=$1 [L]
Then try to load this in browser:
http://www.mysite.com/verify/mysite.com
Try one of the following:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /verify\.php\?site=([^&\ ]+)
RewriteRule ^ /verify/%1 [L,R=301]
Or
RewriteEngine On
RewriteCond %{QUERY_STRING} ^site=([^&]+)
RewriteRule ^/?verify\.php$ /verify/%1 [L,R=301]

Resources