URL rewriting reserved names - apache

In my .htaccess file I have the following rewrite rule:
RewriteRule ^user/(.*) user.php?username=$1 [NC,L]
to take domain.com/user/anything and rewrite it to be used in my application.
However, I want to rewrite it to
domain.com/anything.
The only issue is, there are reserved file names that shouldn't be usernames when accessed, such as index (domain.com/index). What if the user has the name index?

You can use the following rule to exclude existent dirs and files from the rule :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) user.php?username=$1 [NC,L]
If you want to exclude specific uris from the rule, you could use the following instead :
RewriteCond %{REQUEST_URI} !^/user.php [NC]
RewriteRule ^((?!index|upload|foo|bar).+) user.php?username=$1 [NC,L]

Related

how to remove query string from url using htaccess

Once my friend help me to solve this issue, I have written code like that. Where example.com/blog-detail?slug=slugvalue it goes to blog-detail.php and this url example.com/project?slug=test goes to project.php.
RewriteEngine ON
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{THE_REQUEST} \s([^?]*)\?slug=(\S+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$ blog-detail.php?slug=$1 [QSA,L]
I think it's work for only blog-detail page now i face the issue with this url
https://www.example.com/project?slug=test, it redirect me to 404 page, could you help me here.
With your shown samples, attempts; please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
I have added 2 solutions here, so either use 1st rules set OR use 2nd rules set ONLY ONE at a time please.
1st solution: With 2 different rules set for each php file please try following rules.
RewriteEngine ON
###Rules for blog-detail.php redirect and rewrite.
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s(blog-detail)\?slug=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$ blog-detail.php?slug=$1 [QSA,L]
###Rules for project.php redirect and rewrite.
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s(project)\?slug=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$ project.php?slug=$1 [QSA,L]
2nd solution: A Generic solution with capturing groups to deal with both kind of php files together.
RewriteEngine ON
###Rules for blog-detail.php OR project.php redirect and rewrite.
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s([^?]*)\?slug=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ /$1.php?slug=$2 [QSA,L]
NOTE1: Please keep both your php files(project.php and blog-detail.php) should be kept along with your htaccess rules file.
NOTE2: For JS/CS rewrite/redirect:
You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.

URL Rewrite - Multiple query

I am trying to see how I can achieve the following rewrite rules.
From
https://localhost/site/?page=place&place=west
https://localhost/site/?page=location&location=cityname
To
https://localhost/site/place/west
https://localhost/site/location/city
I am able to change
https://localhost/site/?page=place to https://localhost/site/place but not with another additional query as mentioned above.
htaccess
RewriteEngine On
#Redirect /site/?page=foobar to /site/foobar
RewriteCond %{THE_REQUEST} /site/(?:index\.php)?\?page=(.+)\sHTTP [NC]
RewriteRule ^ /site/%1? [L,R]
# Internally rewrite new path to the original one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:site/)?(.+)/?$ /site/?page=$1 [L,QSA]
The above htaccess works for the following which is what I also need.
https://localhost/site/place
https://localhost/site/about
https://localhost/site/contact
With your shown attempts, please try following htaccess rules file. Make sure to clear your browser cache before testing your URLs. New rules are clubbed to your already existing rules.
RewriteEngine ON
RewriteBase /site/
##New rules from here......
RewriteCond %{THE_REQUEST} \s/site/?\?page=([^&]*)&place=([^&]*)\s [NC]
RewriteRule ^ /site/%1/%2? [R=301,L]
# Internally rewrite new path to the original one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^site/([^/]*)/(.*)/?$ index.php?page=$1&location=$2 [NC,QSA,L]

mod_rewrite - how do I match a URL, but not it's subdirectories?

I have an ecommerce site, with product URLs in the following format: site.com/product/long-product-title-here?sku=skugoeshere and want it in this format: site.com/product/long-product-title-here/skugoeshere
One reason for the query is that each page could have multiple variations of the same product.
I'm trying to use mod_rewrite to make it look nicer using the following:
# RewriteCond %{QUERY_STRING} ^sku=(.+)$
# RewriteCond %{REQUEST_URI} product/([^/]+)$
# RewriteRule /([^/]+)$ /$1/%1 [R=301,L]
But this goes into a redirect loop of the form site.com/product/title/title/title...
How do I make the pattern only match the original URL?
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+([^/]+/[^/]+)\?sku=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} !(?:^|&)sku=[^&]+
RewriteRule ^([^/]+/[^/]+)/([^/]+)/?$ /$1?sku=$2 [L,QSA]
skip the leading slash here
RewriteRule /([^/]+)$ $1/%1 [R=301,L]
^ no slash here

Rewrite condition to match ANY file extension

I have the following htaccess rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
What this does is insert a trailing slash character to all my URLs, i.e:
http://localhost/advertising becomes http://localhost/advertising/
Now I have some URLs such as: http://localhost/contact.html
But my htaccess rule is applying the slash to these URLs too - so the above URL becomes http://localhost/contact.html/
Now I added the following condition to prevent this happening:
RewriteCond %{REQUEST_URI} !\.(html|php)$
But I want to be a bit more clever than this - I want a single expression that will match ANY extension the user types in. How can I achieve this?
This will rewrite all directories without trailing slash!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)(\..{3,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]

Apache mod_rewrite query string before page and remove extention

I've scavenged the web for answers to my mod_rewrite woes and I feel I'm at the end of my wits. I will have a URL like such: http://www.website.com/dashboard.php?username=stackoverflow. This url isn't the prettiest of such. So my goal is to do a few things here...
Eliminate the extension of the php file (I've been able to do this so far with the code I'll show below, but I don't know that it will stay according to the other things I need to do)
Eliminate the "www" prefix
Move the username query string (only if it's "username", I don't want to match "id" or such) directly after ".com/"
Move the php filename (without the extension) after query string.
The final URL should look like such: http://website.com/stackoverflow/dashboard or perhaps http://website.com/stackoverflow/profile.
The code I have right now which eliminates the file extension is such:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php\sHTTP/1)
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]
# remove trailing slash ONLY if it is not an existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# rewrite to FILENAME.php if such file does exist and is not a folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]
Sadly this only fixes one of my issues and after looking at it, I'm getting the feeling there would be a better way to do this...
(1, 3, 4) As I understand you want to use pretty URLs, but backed by PHP pages. So it should be like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\w+)/profile$ /dashboard.php?username=$1 [L]
It will make http://website.com/stackoverflow/profile working as http://www.website.com/dashboard.php?username=stackoverflow
(2)
# Redirect www.site.com to site.com with 301
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]