Fix the regex code in .htaccess for beautifying this url - regex

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.

Related

Use .htaccess to redirect to directory URL but show the URL as if in root

I currently have a CRUD that has the following file structure:
The index.php file redirects to the add-product.php, delete.php and other files. The problem is, the URL looks like: myapp.com/resources/views/add-product but I want it to look like myapp.com/add-product. I found other StackOverflow posts similar to this, but it redirects to the desired link, instead of redirecting to the correct link but showing the desired one, and because of that the site doesn't work ("The requested URL was not found on this server"). The .htaccess file looks like this:
Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^resources/(.*)$ /$1 [L,NC,R]
I'm also using the file to be able to remove the .php from the URLs. The last line is the one handling the wrong redirection. How can I go about this? Thank you for your time.
You may use it like this:
Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteBase /myapp/
# external redirect to remove /resources/views/ from URLs
RewriteRule ^resources/views/(.+)\.php$ $1 [L,NC,R=302]
# internal rewrite to add /resources/views/ and .php to show content
RewriteCond %{DOCUMENT_ROOT}/myapp/resources/views/$1.php -f
RewriteRule ^(.+?)/?$ resources/views/$1.php [END]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

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]

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

mod_rewrite rule to change parent folder

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

Tricky URL rewrite, regex .htaccess

I have a tricky issue redirecting some URLs internally for my site.
The situation is this, I currently have a URL like example.com/check/youtube.com which internally redirects to check.php?domain=youtube.com using the following mod_rewrite code:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
RewriteRule ^offline offline.php [NC,L]
RewriteRule ^error error.php [NC,L]
RewriteRule ^check/(.*)$ check.php?domain=$1 [NC,L]
However I would also like to be able to redirect to check.php using a URL like example.com/youtube.com. Unfortunately it is just beyond me to figure it out.
I have a directory /assets/ with all the CSS, JS, etc. which shouldn't be affected.
Thanks
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/.]+\.[^/]+$ check.php?domain=$0 [L]
This rule rewrites any request with a URL path of the form [^/.]+\.[^/]+ (a string that contains at least one dot but no slashes at all) that cannot be mapped to an existing file to your check.php.
As you want to redirect "example.com/youtube.com" does that mean you wish to redirect pretty much anything? What is specifically allowed to be passed, e.g. would I be allowed to pass "example.com/youtube.com/foobar.php" for a redirect to check.php?