Replace some parts of url with htaccess - php

I want to replace some parts of url using htaccess.
I am using below code but i am getting 404 error.
RewriteRule ^/v1/surveys/login(.*)$ /oauth2/rest/token/$1 [R=301,L]
This is my url http://192.168.1.10/survey/api/v1/surveys/login and i want to
replace with http://192.168.1.10/survey/api/oauth2/rest/token

You can use this rule just below RewriteEngine On line:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(survey/api)/v1/surveys/login(/.*)?$ [NC]
RewriteRule ^ /%1/oauth2/rest/token%2 [R=301,L,NE]
# rest of the rules go below this

Related

How to setup rule in .htacces to remove two sub directories

I have setup .htaccess to rewrite some rules in our system and they are all working fine.
Howevever, i am having trouble with the following rule. Can anyone help with this?
I have the following URL
test.com/admin/page/files/TEMPFILES/file.pdf
I would like this to be redirected if the url contains the following two strings "/admin/page/" AND "TEMPFILES"
i would like this to redirect to the following
test.com/files/TEMPFILES/file.pdf
I have tried the following
RewriteCond %{QUERY_STRING} ^TEMPFILES
RewriteCond %{REQUEST_URI} ^admin/([a-z]+)(/.*) [NC]
RewriteRule ^(/.*)?$ $2 [NC,L,QSA]
Any help will be appreciated.
Following URL
test.com/admin/page/files/TEMPFILES/file.pdf
should redirect to
test.com/files/TEMPFILES/file.pdf
You can use this
RewriteEngine on
RewriteCond %{REQUEST_URI} /admin/page/files/TEMPFILES/(.+)\.pdf$
RewriteRule (.*) /files/TEMPFILES/%1.pdf [L,R=301]
This will redirect /admin/page/files/TEMPFILES/foobar.pdf to /files/TEMPFILES/foobar.pdf .

.htaccess; Rewrite folder name in URL

I've many links like this:
www.example.com/my_folder/something/33
www.example.com/my_folder/anything/81
How can I rewrite my_folder to folder123.
Means: If I type www.example.com/folder123/something/33 I'm getting the content of www.example.com/my_folder/something/33 but the URL remains www.example.com/folder123/something/33.
How can I do that with .htaccess?
You can do this with a simple rewrite rule as this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+my_folder(/\S*)?\s [NC]
RewriteRule ^ /folder123%1 [R=301,L,NE]
RewriteRule ^folder123(/.*)?$ my_folder$1 [L,NC]

.htaccess; Too many redirects if two different redirects in .htaccess

I've two types of URLs:
http://www.example.com/?content=contact_form.php
and
http://www.example.com/?content=product.php&id=20
I changed my whole URL system like this:
http://www.example.com/file/contact_form
and
http://www.example.com/product/I-m-the-title/20
Of course I made 301 redirect with .htaccess to tell Google and co. the new URL.
I made it like this:
# Rewrite URLs
RewriteEngine On
RewriteRule ^(ignore)($|/) - [L]
RewriteRule ^file/([^/]*)$ /?content=$1.php [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /?content=$1.php&title=$2&id=$3 [L]
# Redirect old URL to new URL
RewriteCond %{QUERY_STRING} ^content=contact\_form\.php$
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule .* http://www.example.com/file/contact_form? [R=301,L]
RewriteCond %{QUERY_STRING} ^content=product\.php&class=I-m-the-title$
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule .* http://www.example.com/I-m-the-title/Test/20? [R=301,L]
My problem:
It's perfectly working for: http://www.example.com/?content=product.php&id=20
But for http://www.example.com/?content=contact_form.php I'm getting the message that it couldn't get opened because of too much redirect.
Does anybody know what I'm doing wrong? I hope anybody can help me soon because I have to fix it before Google misinterprets it.
Your rule cause an infinite loop because it is rewriting your uri to the same location again and again overriding your internal and external redirects.. To fix the Rewrite loop, add the following at the top of your htaccess
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

Rewrite rule then redirect

Hello I am trying to rewrite my url
http://example.net/?r=123
to this one
http://example.net/ref/123
In my .htaccess this is working using this code
RewriteEngine on
RewriteRule ^ref/([^/.]+)/?$ /index.php?r=$1 [R=301]
But i would like to add another rewrite rule. When the link is like this:
http://example.net/?r=123
I would like to redirect it to:
http://example.net/
I tried this rewrite rule but it's not working:
RedirectMatch "^/?r=([^/.]+)" "/index.php"
Please help. Any help will be very much appreciated. Thank you so much.
Try these rules to redirect the query string URL to the pretty URL which is more common. Basically redirecting the old url to the SEO friendly one.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /+\?r=([^&\s]+)\
RewriteRule ^ /ref/%1? [R=302,L]
RewriteRule ^ref/([^/.]+)/?$ /index.php?r=$1 [R=301]
If you still want to redirect the query string to index.php instead try this rule
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /+\?r=([^&\s]+)\
RewriteRule ^ /index.php [R=302,L]
RewriteRule ^ref/([^/.]+)/?$ /index.php?r=$1 [R=301]

How to rewrite subsites with parameter to html only

My old website has a url like "/index.php?s=subsite" and "/?s=subsite".
Now I added the following lines to ".htaccess" file, to make them shorter:
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)\.(html)?$ index.php?s=$1&%{QUERY_STRING} [L]
Open a site with "/subsite.html" works fine, but also the old url "/index.php?s=subsite" and "/?s=subsite" works too. Is there a way, to allow "/subsite.html" only and redirect the old requests to it?
Replace your code with this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:index\.php)?\?s=([^\s]+) [NC]
RewriteRule ^ /%1.html? [R=301,L]
RewriteRule ^([^.]+)\.html$ /index.php?s=$1 [L,NC,QSA]
Add this rule to your htaccess file:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?s=([^& \]+)&?([^\ ]*)
RewriteRule ^ /%1.html?%2 [L,R=301]
This matches against the request, tries to capture the s query string parameter, then redirects the browser to that parameter followed by an ".html" and the rest of any query string that was there.

Categories