mod_rewrite rule to change parent folder - regex

Some unchangeable hyperlinks on a website point to /folderA/index.php?id=somestuff.
I need to redirect the Request to /folderB/index.php?id=somestuff instead.
I did some experimenting with this but I just can't get it to work, any help is appreciated.
RewriteEngine on
RewriteRule /folderA/index\.php\?id=([\w-]+)$ /folderB/index.php?id=$1

Keep in mind that RewriteRule doesn't match query string and matches only REQUEST_URI without it. Use this code instead:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folderA/(index\.php\?id=[^&\s]+) [NC]
RewriteRule ^ /folderB/%1 [R=302,L]

Assuming that you are not planning on using different get request this should work:
RewriteEngine on
RewriteRule /folderA/index\.php\?id=(.*?)$ /folderB/index.php?id=$1

Related

Is there any way to redirect to a clean url when old url is requested?

I want to change the URLs on my website from page.php?id=1&name=john to page/1/john using htaccess RewriteRule.
This is what I have currently but it is not working as expected:
RewriteRule ^page/([0-9]+)/([a-zA-Z\s-]+) page.php?id=$1&name=$2
Is it possible to make this change in htaccess rules or should I change every link to <a href='page.php/1/john'>Page</a> which is tiresome since I have got many links in every page. Help is appreciated. Thanks.
You may use these 2 rules in site root .htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /rootfolder/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /page\.php\?id=(\d+)&name=([^\s&]+)\s [NC]
RewriteRule ^ page/%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^page/(\d+)/([^/]+)/?$ page.php?id=$1&name=$2 [L,QSA,NC]
1st solution: To get from query string URL to user friendly url try following, as per OP's request.
Options -MultiViews
RewriteEngine On
RewriteBase /rootfolder/
RewriteCond %{QUERY_STRING} ^id=(\d+)&name=([\w-]+)/?$ [NC]
RewriteRule ^([^.]*)\..*$ /$1/%1/%2/ [QSD,R=302,NC,L]
2nd solution: As far as I get from OP's question, could you please try following. Considering as per thumb rule users will be given friendly URL like eg--> http://localhost:80/page/1/john and it will point in pointed to http://localhost/page.php?id=1&name=john in backend.
Options -MultiViews
RewriteEngine On
RewriteBase /rootfolder/
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]*)/([^/]*)/(.*)/?$ /$1.php?id=$2&name=$3 [NC,L]

Fix the regex code in .htaccess for beautifying this url

my current regex code to beautify a url (www.abc.com/user.php?url="rio") in .htaccess file is
RewriteCond %{REQUEST_URI} user
RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)$ user.php?url=$2
RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)/$ user.php?url=$2
It works fine and the page url is beautified to :
www.abc.com/user/rio
but there is a problem now, Now even if I change url like in following ways:
www.abc.com/user/user/user/rio
www.abc.com/user///rio
www.abc.com/user/abc/rio
These links work same way, and that is making content duplication.
Please help!
You may try these rules in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# remove multiple slashes
RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ /$0 [R=301,L,NE]
# handles /user/something
RewriteRule ^user/([\w-]+)/?$ user.php?url=$1 [L,QSA,NC]
Options -MultiViews is required to turn off content negotiation feature.

RewriteRule when there is ?id=

I try to write a RewriteRule in the .htaccess but I have problems
I try to redirect from:
blog/entrada.php?id=2
To:
/blog/3D-touch
This is one of the multiple things I tried and does not work:
RewriteRule ^blog\/entrada\.php\?id=2$ /blog/3D-touch [L,R=301]
What is wrong with my Rule. How to redirect effectively?
Thanks
Querystring is not part of match in RewriteRule directive, to redirect query strings, you need to use RewriteCond one of the following options :
option 1
RewriteEngine on
RewriteCond %{THE_REQUEST} /blog/entrada\.php\?id=2 [NC]
RewriteRule ^ /blog/3D-touch? [NC,L,R]
option 2
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=2$ [NC]
RewriteRule ^blog/entrada\.php$ /blog/3D-touch? [NC,L,R]
We use an empty question mark ? at the end of the target url to discard the old query strings, otherwise these query strings get appened to the target url by default.
Change the R to R=301 if you want to make the redirection permanent.
use from this code
RewriteRule blog/(.*) blog/entrada.php?id=$1
this code will redirect all urls which have blog/ to blog/entrada.php and put after value of blog/ to $_GET['id']
you should have following code n the top location of your htaccess file
Options +FollowSymLinks
RewriteEngine on

[domain]/? redirect to [domain] using .htaccess

We are receiving traffic from an important source but they are sending them to our domain with a /? at the end of it. I have tried to redirect with an entry in our .htaccess but this doesn't work:
Redirect /? http://[domain]
RewriteEngine on
RewriteBase /
I also tried backslashing it:
Redirect /\? http://[domain]
RewriteEngine on
RewriteBase /
Anyone know how to get this to work?
You don't need Redirect directive as more advanced mod_rewrite is needed for this case.
Try this rule:
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+([^?]*)\?\s [NC]
RewriteRule ^ /%1? [R=302,L]

Mod Rewrite and 404 Error

At the moment the users of my website can access files with the following url-schema:
http://www.example.com/dwl/download.php?file=/files/index.rar
Now i want that they can access it with the following schema:
http://www.example.com/dwl/content/files/index.rar
The download.php file is within the dwl directory.
I tried the following .htaccess:
RewriteEngine On
RewriteRule ^file/([^/]*)$ download.php?file=$1 [L]
But when i try it, i get a 404-Error.
Can anybody please help me to write the correct .htaccess-Entry?
If you want to redirect from http://www.example.com/dwl/download.php?file=/files/index.rar to http://www.example.com/dwl/content/files/index.rar, you need this:
RewriteRule ^/dwl/download.php?file=/(.*) /dwl/content/$1 [L]
I think the code you have to use is the following :
RewriteEngine On
RewriteRule ^/dwl/content(/files/[^/]*)$ /dwl/download.php?file=$1
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT/dwl directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /dwl/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+dwl/download\.php\?file=([^\s&]+) [NC]
RewriteRule ^ content/%1? [R=301,L]
RewriteRule ^content/(.+)$ download.php?file=$1 [L,QSA,NC]