I have a RewriteRule in my .htaccess that works almost as I wanted.
What I want is a rule that works with an optional trailing slash.
foo.com/bar > foo.com/index.php?p=bar
foo.com/bar/ > foo.com/index.php?p=bar
What I have is this;
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.pdf)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/?$ index\.php\?p=$1 [NC,L]
But this doesn't seem to work when the trailing slash is added.
You may use this rule to allow an optional trailing slash:
RewriteCond %{REQUEST_URI} !\.(png|jpe?g|gif|bmp|pdf)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?p=$1 [QSA,L]
Related
I have two kinds of routes.
This works:
http://localhost/monitor/test1
This does not:
http://localhost/monitor/test2/test3
Here is my current rule:
RewriteRule (^[^/]*$) public/$1 [L]
I know this one only matches the last slash. How can I change the regex to match both cases?
1st solution: Could you please try following.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/monitor[^/]*/(.*)$
RewriteRule ^.*$ /public/%1 [NC,L]
OR in case you could have any other string other than monitor in REQUEST_URI starting part then please try following.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]*/(.*)$
RewriteRule ^.*$ /public/%1 [NC,L]
2nd solution: Or we can catch values in while writing RewriteRule itself and could reduce 1 RewriteCond which is used in 1st solution here.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]*/(.*)$ /public/$1 [NC,L]
OR with monitor matching keyword:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^monitor[^/]*/(.*)$ /public/$1 [NC,L]
I currently have my website excluding the .html using the following code in .htaccess:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
And this works great, but if I add a trailing slash I get a 404.
I'd like to have the option of having it work as it is now and also if I add a trailing slash at the end.
Is this possible?
Thanks
Sure you can use:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]
I need help with the following .htaccess, i need to combine them together if its possible
This will redirect to the domainname.com.au with http://www at the front of domain name if there is no http:// or www at the front:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domainname.com.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domainname.com.au$
RewriteRule ^(.*)$ "http\:\/\/www\.domainname.com.au\/$1" [R=301, OR]
And this one should get the variables that passing at the end of the domain name after slash /, I should see "/contact" but the php MVC see this "/index.php?url=contact"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
deny from 111.93.163.217
Thanks!
The two rules can't be combined as they're quite different, but they can be shortened:
# Force www
RewriteCOnd %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Rewrite parameter
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [B,NE,QSA,L]
I have this url: http://example.org/product.php?course_id=$x
And I want this url changes to this: http://example.org/Course-Name
this is my code in .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ 404page.php
RewriteRule ^/([0-9]+)/([^/]+)/$ /product.php?course_id=$1 [L,R]
Which is not working kindly tell me whats wrong with my code?
You need to remove leading slash from your rule. Try this rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+product\.php\?course_id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /product.php?course_id=$1 [L,QSA]
i'm trying to solve a mod_rewrite rule for my webshop.
The URL is: localhost/myshop/category1/category2/myproduct.html
The rediret URLshould be: localhost/myshop/configurator/product/configure/id/1/s/myproduct/category1/5/
If I try a RewriteRule like this
RewriteRule ^myproduct\.html$ http://localhost/myshop/configurator/product/configure/id/1/s/myproduct/category1/5/
nothing happens and the redirect will not appear.
What i'm doing wrong?
my rewrite rules in .htaccess
RewriteBase /myshop/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
RewriteRule ^myproduct\.html$ http://localhost/myshop/configurator/product/configure/id/1/s/myproduct/category1/5/
I want that myproduct.html redirects immediatly to the configurator link, so i want to implement a RewriteRule
Try re-ordering your rules:
RewriteEngine On
RewriteBase /myshop/
RewriteRule (^|/)myproduct\.html$ /myshop/configurator/product/configure/id/1/s/myproduct/category1/5/ [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]