I've got this setup:
RewriteRule ^brands$ brands/ [R=301,L]
RewriteRule ^brands/$ /brands.php
RewriteRule ^brands/([A-ZÆØÅæøåa-z0-9-]+)-([0-9]+)/$ index.php?manufacturers_name=$1&manufacturers_id=$2 [L,QSA]
RewriteRule ^brands/([0-9]+)$ index.php?manufacturers_id=$1 [L]
How would I fix it so there's alway a trailing slash on this - Those specific urls?
xxx.com/brands/brand-id
So if I either went to xxx.com/brands/brand-id OR xxx.com/brands/brand-id/ - It'll work as having a trailing slash?
Replace your Rule with this
RewriteRule ^brands/([0-9]+)/?$ index.php?manufacturers_id=$1 [L]
/? means that the / is optional in uri. Your rule will match both uri strings ending with or without a traling slash.
And to add the traling slash to specific uris,put the following before your existing rules
RewriteRule ^brands/[0-9]+$ %{REQUEST_URI}/ [L,R]
This will redirect /brands/123 to /brands/123/ .
Related
Trying to achieve rewrites for 3-1 paths in the url, and then a rewrite for index. Can achieve the former with below:
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?$ /index.php?a=$1&b=$2&c=$3 [L,QSA,NC]
RewriteRule ^([^/]+)/([^/]+)?$ /index.php?a=$1&b=$2 [L,QSA,NC]
RewriteRule ^([^/]+)?$ /index.php?a=$1 [L,QSA,NC]
Want to add after this a rewrite from index (eg "www.domain.com") to /index.php?a=10 but cant get that work.
RewriteRule ^([^/]+)?$ /index.php?a=$1 [L,QSA,NC]
The "problem" is that the last rule also matches an empty URL-path so the request is rewritten to /index.php?a= and any later rule that matches the root is not processed.
Instead of writing the rule to match the root "after" the existing rules, you can add it before your existing rules to avoid conflict. For example:
RewriteRule ^$ /index.php?a=10 [QSA,L]
: other rules follow...
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?$ /index.php?a=$1&b=$2&c=$3 [L,QSA,NC]
RewriteRule ^([^/]+)/([^/]+)?$ /index.php?a=$1&b=$2 [L,QSA,NC]
RewriteRule ^([^/]+)?$ /index.php?a=$1 [L,QSA,NC]
Note that the RewriteCond directive only applies to the first RewriteRule directive. Do you need this at all?
Your rules are a little ambiguous since in each of the regex you are allowing the last path segment to be optional. This basically means the trailing slash is optional (eg. /foo/bar/ and /foo/bar are both valid URLs according to these rules). If you don't make the path segment optional in the last rule then you can place your rule to match the root last as you were wanting to do originally.
A request for /foo/bar/baz matches the first rule. /foo/bar/ (with a trailing slash) also matches the first rule, but /foo/bar (no trailing slash) matches the second rule. Likewise, /foo/ also matches the second rule, but /foo matches the third rule. And (following the same pattern) the third rule also matches / (hence your initial problem). My point is... should that last path segment on each rule be optional?
There are 2 RewriteRules for permalink in php.
RewriteRule ^(.*).htm index.php [NC,QSA,L]
RewriteRule ^(.*).html index.php [NC,QSA,L]
I need to browse a static html file:
/uploads/themes/mail-signature/mail-signature.html
But when I enter http://example.com/uploads/themes/mail-signature/mail-signature.html it's showing index.php.
How can I discard these rules?
RewriteRule ^(.*).htm index.php [NC,QSA,L]
RewriteRule ^(.*).html index.php [NC,QSA,L]
Your 2nd rule isn't actually doing anything anyway, since any .html requests will be caught by the first rule. So, the second rule can be removed.
You then add a condition to the first rule that excludes the specific URL you are trying to access. For example:
RewriteCond %{REQUEST_URI} !^/uploads/themes/mail-signature/mail-signature\.html$
RewriteRule \.html?$ index.php [NC,L]
The QSA flag is not required here, as any query string will be passed through by default.
The ^(.*) prefix on your RewriteRule pattern is not required since you aren't using this backreference.
You should backslash escape any literal dots in the regex, eg. \.htm, otherwise you are matching any character, eg. "xhtm". And presumably you only want to match URLs that end in .htm or .html? Your original regex would match "htm" anywhere in the requested URL.
I'm sorry if my question is unclear. I rewrite rule url by htaccess and it worked. So i have a problem when i try to link path of the existing folder.
My problem:
Redirect url: 'http://localhost/folders'
But it display: 'http://localhost/folders/?link=folder'
So, I don't want it show '?link=folder'. It not show '?link=folder' with Redirect url: 'http://localhost/folders'
My htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^$ index.php [L]
RewriteRule ^([^/]*)/$ index.php?link=$1&%{QUERY_STRING} [L]
RewriteRule ^([^/]*)/([^/]*)/$ index.php?link=$1&action=$2&%{QUERY_STRING} [L]
RewriteRule ^([^/]*)/([0-9]+)/([^/]*)/$ index.php?link=$1&id=$2&action=$3&%{QUERY_STRING} [L]
RewriteRule ^([^/]*)$ index.php?link=$1&%{QUERY_STRING} [L]
RewriteRule ^([^/]*)/([^/]*)$ index.php?link=$1&action=$2&%{QUERY_STRING} [L]
RewriteRule ^([^/]*)/([0-9]+)/([^/]*)$ index.php?link=$1&id=$2&action=$3&%{QUERY_STRING} [L]
RewriteRule !^(public/*|folder/*|index\.php) [NC,F]
Please, someone tell me how to fix. I'm sorry if my english is bad.
Your problem maybe to do with the DirectorySlash. If "folders" is a physical directory on the filesystem and you request http://localhost/folders (no trailing slash) then mod_dir "fixes" the URL by 301 redirecting to http://localhost/folders/ (with a trailing slash).
This mod_dir behaviour conflicts with the following rule that appends the ?link=folders quesy string.
RewriteRule ^([^/]*)$ index.php?link=$1&%{QUERY_STRING} [L]
This "rewrite" will be turned into an external "redirect" by mod_dir.
You can try preventing mod_dir from appending the trailing slash on directories with the following directive at the top of your .htaccess file:
DirectorySlash Off
However, you now need to manage all the trailing slashes yourself, which may not be trivial.
However, instead of the above, I would simply include a trailing slash on the URL to begin with and only match URLs that have the trailing slash, rather than both (you are duplicating your directives).
I've tried several versions of trailing slash code but none of them are working for me. I want a trailing slash to be added only when url contains a keyword news, so
domain.com/news
domain.com/news/category/foo
domain.com/news/archive/august-2018
should trigger it to add a slash. I have this right now
RewriteCond %{REQUEST_URI} ^/(news.*)$
RewriteRule ^/news(.*[^/])$ %{REQUEST_URI}/ [R=301,L]
Try using below rule,
RewriteRule ^news$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301]
You can do this using two separate rules, one for just news on it's own, and another for news/categroy/foo:
# direct news to news/
RewriteRule ^news$ news/ [L,R=301]
# if no trailing slash, direct news/* to news/*/
RewriteCond %{REQUEST_URI} ^(.*[^/])$
RewriteRule ^news/(.*)$ news/$1/ [L,R=301]
These three rules result in:
http://www.example.com/news => http://www.example.com/news/
http://www.example.com/news/category/foo => http://www.example.com/news/category/foo/
http://www.example.com/news/archive/august-2018 => http://www.example.com/news/archive/august-2018/
You can test these rules using htaccess.madewithlove.be.
My .htaccess file always requires URL with trailing slash such as:
http://localhost/menjaraz/webroot/about/
http://localhost/menjaraz/webroot/2013/03/21/you-are-my-heart-you-are-my-soul/
to work, otherwise a 404 error is fired.
I wish to loosen the rule(s) so that an URL without an ending slash will also do as well.
Excerpt
RewriteBase /menjaraz/webroot
RewriteRule ^([^/]*)/$ index.php?page=$1
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ index.php?a=$1&b=$2&c=$3&page=$4
How should I do that ? I'm not very comfortable neither with regex nor with mod_rewrite.
Thanx in advance.
You need to make the last slash optional using the ?:
RewriteBase /menjaraz/webroot
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^([^/]*)/?$ index.php?page=$1
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ index.php?a=$1&b=$2&c=$3&page=$4
The ? makes the character or group right before it optional. Because there's no trailing slash, you need to add a condition to make sure index.php doesn't get rewritten.