I'm new to htaccess and i want to use RewriteRule in order to make urls more friendly but i have a problem.
Here is my htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule download/(.*)$ download.php?file=$1 [NC]
RewriteRule download/view/(.*)/(.*)$ download_view.php?name=$1&id=$2 [NC]
</IfModule>
Whatever i navigate to localhost/download/view/test/2/ for example, it appears as the first url localhost/download/test/
How to fix this ?
Turn around the order of those two rules, so that the more specialized one comes first. Then use the additional L flag to prevent further rewriting after a match:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule download/view/(.*)/(.*)$ download_view.php?name=$1&id=$2 [NC,L]
RewriteRule download/(.*)$ download.php?file=$1 [NC]
</IfModule>
Your first rewrite-rule already changes the URI:
RewriteRule download/(.*)$ download.php?file=$1 [NC]
Therefore localhost/download/view/test/2/ is already rewritten to download.php?file=view/test/2/ and the second rule you intentionally want to match against does not match any longer:
RewriteRule download/view/(.*)/(.*)$ download_view.php?name=$1&id=$2 [NC]
This does not match download.php?file=view/test/2/.
So you can solve this, by doing the check for download/view/... first, by flipping those two:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule download/view/(.*)/(.*)$ download_view.php?name=$1&id=$2 [NC]
RewriteRule download/(.*)$ download.php?file=$1 [NC]
</IfModule>
Always start with the most concrete/specific rule to rule out such problems. In more difficult cases, use RewriteCond to fine-tune when to apply a rewrite-rule.
I also suggest if you want to match at the beginning of the URI Paths, you should use the caret ("^") to mark the beginning in the regular expression:
RewriteRule ^download/view/(.*)/(.*)$ download_view.php?name=$1&id=$2 [NC]
RewriteRule ^download/(.*)$ download.php?file=$1 [NC]
Related
I am trying to redirect my subpage to a non-www but I get duplicate URLs of the same page.
What I want to implement
https://www.example.com/category/webs => https://example.com/category/webs
http://example.com/category/webs => https://example.com/category/webs
http://www.example.com/category/webs => https://example.com/category/webs
.htaccess
Options -Indexes
RewriteEngine On
RewriteRule ^category/([a-zA-Z0-9-/]+) category-list.php?id=$1 [NC,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
</IfModule>
With this current code, my root domain redirects perfectly. but not subpages! I read many articles but not helping
The rule you have posted already does this, but your rules are in the wrong order. You need to redirect the request before rewriting the request to category-list.php. Otherwise, "subpages" will be redirected to category-list.php, which is what I assume is happening.
Aside: You should escape the literal hyphen in the character class ([a-zA-Z0-9-/]+), or move this to the start or end of the character class, to negate its special meaning. You should also include the end-of-string anchor at the end of the regex, otherwise it will successfully match too much (potentially creating duplicate content).
Try it like this instead:
Options -Indexes
RewriteEngine On
# Canonical redirect
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
# Rewrite request
RewriteRule ^category/([a-zA-Z0-9/-]+)$ category-list.php?id=$1 [NC,L]
No need to repeat the RewriteEngine directive. No need for the <IfModule> wrapper.
I want to create pretty url. But, I got some problem with .htaccess. For example I have url domain/some.php?f=query-string.
I want to change domain/query-string (expected url). Is that possible to change / redirect via .htaccess. Or maybe from php file itsself.
this is a bit of htaccess snippet i made, but i get it blank/error page
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteRule ^/([^/.]+)$ some.php?f=$1 [NC,L]
</IfModule>
Thanks for your attention.
RewriteRule ^/([^/.]+)$ some.php?f=$1 [NC,L]
In .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash, so the above will never match and it will do nothing. This should be written like the following instead:
RewriteRule ^([^/.]+)$ some.php?f=$1 [L]
The NC flag is not required here, since the regex is already "case-insensitive".
I need to replace underscores to dashes. I have tried this code and tried to change some parameters here, but i'm not yet friedly with .htaccess syntax so i failed to do it myself:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule !\.(html|php)$ - [S=6]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6-$7 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=underscores:Yes]
RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=underscores:Yes]
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule (.*) /$1 [R=301,L]
My previous .htacces code was this, it was working fine, but there was no need to replace underscores to dashes till now:
# Added for SME compatibility
Options +FollowSymLinks
# Ignore Indexation of apache
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
# Rules for ReRouting to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
I have URL like this: http://mydomain.tld/nl_BE/get_quote , the language (1st parameter) should NOT be replaced with dash, also language parameter can change, so i cannot hardcode language name in .htaccess file.
So i want to have URL something like this:
http://mydomain.tld/nl_BE/get-quote/some-random-title
http://mydomain.tld/nl_NL/about-us/some-random-text
Thanks in advance!
Rather than those multiple rules you can use this generic recursive rule:
Options +FollowSymLinks
RewriteEngine On
## RECURSION based rule
# if there is one underscore left then redirect
RewriteRule ^([a-z]{2}_[a-z]+)/([^_]*)_([^_]*)/?$ /$1/$2-$3 [NC,L,R=302]
# if there are more than one underscores then "repeatedly" replace it by -
RewriteRule ^([a-z]{2}_[a-z]+)/([^_]*)_(.*)$ $1/$2-$3 [L,NC]
Trying to redirect this always works:
RewriteRule ^user/(.*) view_channel.php?user=$1 [nc]
The problem, trying to redirect the statement below alone works but when combined with the above statement it doesn't work (the result was a blank page):
RewriteRule ^user/videos/(.*) user_videos.php?user=$1 [nc]
More details about .htaccess:
Options All -Indexes
FileETag MTime Size
Options +FollowSymlinks -MultiViews
RewriteEngine on
<FilesMatch "\.(db|inc|tmpl|h|ihtml|sql|ini|configuration|config|class|bin|spd|theme|module|cfg|cpl|tmp|log|err|inc.php|class.php)$">
order allow,deny
satisfy all
</FilesMatch>
What am I doing wrong?
It is because your regex ^user/(.*) also matches a URI like /user/videos/something.
Have your rules like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^user/videos/([^/]+)/?$ user_videos.php?user=$1 [L,NC,QSA]
RewriteRule ^user/([^/]+)/?$ view_channel.php?user=$1 [L,QSA,NC]
The rules are not mutually incompatible. Nor are they final. Change them like this:
RewriteRule ^user/([^/]+) view_channel.php?user=$1 [NC,L]
RewriteRule ^user/videos/([^/]+) user_videos.php?user=$1 [NC,L]
[^/]+ matches one or more character that is not a /. This ensures that the first rule cannot match the second pattern, which it did with the .*, as that matches any characters, including / of course.
The second RewriteRule statement is matched against already rewritten url.
I want to remove 'articles' and 'artificial' folders from a site's urls, but when I try to introduce rewrite rules into .htaccess file I am receiving errors.
http://www.example.com/articles/artificial/are_string_beans_all_right_on_the_candida_diet.php
trying to convert
http://www.example.com/are_string_beans_all_right_on_the_candida_diet.php
here is the current version of my .htaccess file
DirectoryIndex index.php
AddDefaultCharset utf-8
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php/?$ / [L,R=301,NC]
RewriteRule ^((?!articles/artificial).*)$ articles/artificial/$1 [NC,L]
The problem should be on the last rule: what you need is a negative look behind ie anything not preceede with articles/artificial
The regex should be:
((?<!(articles/artificial)).*)
But this probabilly does not work since the .* is variable length (see regex look arrounds)
A solution could be replacing the negative look behind with a positive look ahead that allows for variable length. So your rule could be:
RewriteRule ^(?!.*?articles/artificial.*?)(.*)$ /articles/artificial/$1