Htaccess Rewrite rule for removing a dot - .htaccess

Thanks for your help. I have been trying to solve this for an hour now.
I had this Htaccess that would rewrite /file.html to a script but decided to remove the extension and it turned out like this:
RewriteEngine On
#RewriteRule ^([^/]*)\.html$ /?file=$1 [L]
RewriteRule ^([^\.]+)$ /index.php?file=$1 [NC,L]
However, now I need it to work also with filename containing the dot (i.e. /file.with.dots) but that rule doesn't allow it.
This one works for files with dots but ending in .html
#RewriteRule ^(.*)\.html$ /index.php?file=$1 [NC]
This gets me an Internal Server Error
RewriteRule ^([^/]+)$ /index.php?file=$1 [NC,L]

You could just do this.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/?$ /index.php?file=$1 [NC,L]
Let me know the result.

Related

Rewrite my URL to remove strings using htaccess

I need help with my htaccess file. I added the following to remove all .php extensions which is working fine.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
However I have a directory /users which I want to rewrite the URL strings. The current URL is like this:
aero.site/maki/users/index.php?t=mf5cc
I want the URL to look like
aero.site/maki/mf5cc
I used the following rewrite rule but it's giving me Object not found error:
RewriteRule ^([^/.]+)$ users/index.php?t=$1 [L]
My complete htaccess content is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^/.]+)$ users/index.php?t=$1 [L]
Kindly help me with the correct rewrite rule for the last line to turn aero.site/maki/users/index.php?t=mf5cc into aero.site/maki/mf5cc
FYI: my htaccess file is in aero.site/maki directory
I finally got it to work. Thanks to #misorude. Here's my updated .htaccess content:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^/.]+)$ users/index?t=$1 [L]

URL re-writing using .htaccess

I have a URL:
domain.com/abc/hotel_detail.php?id=2
And I want to do a URL re-write to make it look like this :
domain.com/abc/hotel_detail/2
My current .htaccess looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{QUERY_STRING} ^id=20$
RewriteRule ^abc/hotel_detail/([0-9]+)$ ^abc/hotel_detail.php?id=$1 [L]
The first part contains code for removing the extension(.php) from pages.
When I try to open this link domain.com/abc/hotel_detail/2 , it gives me object not found error.
Can Someone please tell me whats wrong with the code?
Remove this line:
RewriteCond %{QUERY_STRING} ^id=20$
Changing your file to:
RewriteEngine On
RewriteRule ^abc/hotel_detail/([0-9]+)$ ^abc/hotel_detail.php?id=$1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,R]
should work. I removed RewriteCond %{QUERY_STRING} ^id=20
Your first rewritecond and rule was overwriting the second one, switching them around should fix it.
I think this should work
RewriteRule ^abc/hotel_detail/([0-9]*)$ abc/hotel_detail.php?id=$1 [L]
([0-9]*) miltiply sign.
and the ^ for the real link

.htaccess rule not excluding existing files when rule has a slash condition

I am redirecting this type of url
http://www.site.com/windows/games/ or http://www.site.com/windows/games
http://www.site.com/windows/software/ or http://www.site.com/windows/software
to
http://www.site.com/windows/ct.php?ct=games
http://www.site.com/windows/ct.php?ct=software
Site structure
wamp/www/Site/windows/ct.php
I am trying this way its redirecting properly but when url has trailing slash at end its rewriting css, js files too.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^windows/([^/]*)/?$ /Site/windows/ct.php?ct=$1 [L]
i.e
this type of url has css and javascript applied.
http://www.site.com/windows/games
but this type of url has no css and javascript applied. (not working with trailing slash).
http://www.site.com/windows/games/
I have tried several syntax like
RewriteRule ^windows/([^/]+)/?$ /windows/ct.php?ct=$1 [L]
RewriteRule ^windows/(^(.[^/]*)/?)$ /windows/ct.php?ct=$1 [L]
RewriteRule ^windows/([^/]*)/? /windows/ct.php?ct=$1 [L]
RewriteRule ^windows/([^/]+)/? /windows/ct.php?ct=$1 [L]
but it didn't work.
Complete .htaccess
Options +FollowSymlinks -MultiViews
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^windows/([^/]*)/?$ /Site/windows/ct.php?ct=$1 [L]
RewriteRule ^[^/]*/[^/]*/(.*\.html) /Site/error/400/ [L]
RewriteRule ^(error) - [L]
RewriteRule ^sitemap\.xml$ sitemap.php [L]
RewriteRule ^rss/(.*?)\.xml$ rss/$1.php [L]
</IfModule>
ErrorDocument 404 /Site/error/404/
Please see and suggest any possible way to do it.
Thanks.
The issue here is that your relative paths break when you add a trailing slash as it introduces two-levels of directory structure while the CSS/JS file paths come out only once ... So, your RewriteRule is actually firing for CSS/JS files as well when resolving 404s for paths like /windows/global/js/js.js. But, the scripts still fail to work because they aren't under /Site and that's where your rule is serving the request from.
So, to fix things without touching the relative URLs or resource locations; add another rule as follows:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^windows/([^/]*)/?$ /Site/windows/ct.php?ct=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^windows/(global/.*)$ /$1 [L] # handles css|js files
The rest of your .htaccess (of course) remains the same.
I use the following rule in .htaccess, but this may be too general:
RewriteRule ^q/([^/]+)/([^/]+)(\/?)$ /path/to/q.php?type=$1&q=$2&{QUERY_STRING} [NC]
This may be better for you:
RewriteRule ^windows(\/?)$ /path/to/windows.php?{QUERY_STRING} [NC]
RewriteRule ^windows/([^/]+)(\/?)$ /path/to/windows.php?type=$1&{QUERY_STRING} [NC]
You will have to change this according to your situation. I haven't tested this, but I hope it is good enough for you to get where you want.

mywebsite.com/project.php?id=123 should redirect via .htaccess to mywebsite.com/123

mywebsite.com/123 should display content via .htaccess of mywebsite.com/project.php?id=123 - unfortunately I changed the .htaccess rewriterule yesterday for anothe reason and now the redirection does not function (it goes to our 404-error page). Can you see what I've done wrong here? Many thanks!
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9\-_]+)/?$ $1.php [NC,L]
Changing the last line to this should work:
RewriteRule ^([a-z0-9\-_]+)/?$ project.php?id=$1 [NC,QSA,L]
if you want the regex rule not to catch URLs that contain alphabetic characters, use
RewriteRule ^([0-9\-_]+)/?$ project.php?id=$1 [NC,QSA,L]
And for your new requirement:
RewriteRule ^([0-9\-_]+)/?$ project.php?id=$1 [NC,QSA,L]
RewriteRule ^([a-z\-_]+)/?$ new.php?id=$1 [NC,QSA,L]

mod_rewrite - do two redirects - if the first one dosen't match do the second

This is my htaccess file at the moment
RewriteEngine On
# Only redirect if file dosen't exist.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin/(.*) /admin.php
RewriteRule ^([^/]) /index.php [L]
I don't have any idea why this doesn't work. I think I've finally grasped mod_rewrite and then it just does something completely unexpected like this.
Basically if the URL is domain.com/admin/something then I want it to redirect to domain.com/admin.php (including if its just /admin). However if its ANYTHING else I want it to redirect to index.php.
Any idea how to achieve this?
Thank you.
RewriteEngine On
# No redirect, if file or directory exists
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^admin/(.*) /admin.php [L]
RewriteRule ^(.*)$ /index.php [L]
Didnt test it. The interesting part is the L-Flag after the admin-rule, because it prevents the next rule from matching.
I changed the RewriteCond-Statements, because they only apply to the one next RewriteRule and (in your case) doesnt affect the rule to index.php.

Resources