I have a two Url rule
RewriteRule ^/user/strategies$ /user/index.html [NC,L]
RewriteRule ^/user/([a-zA-Z_0-9.]+)$ /user/index.html?username=$1 [NC,L]
You can see that they both contradict each other, what is the best possible way to solve this problem.
You can have these rules like this:
RewriteEngine On
RewriteRule ^user/strategies$ /user/index.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([a-zA-Z_0-9.]+)$ /user/index.html?username=$1 [NC,L]
.htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.
Related
I am using rewrite rules for two purposes. First, I am able to rewrite the URL's to my .css and .js files so they can be processed through a minimize script. This has been working fine:
RewriteEngine on
RewriteRule ^assets/css/min/([a-zA-Z0-9\._-]+)/([a-zA-Z0-9\._-]+)/([a-zA-Z0-9\._-]+)\.css$ assets/css/min.php?style=$1&theme=$2&ver=$3 [L,QSA]
RewriteRule ^assets/js/min/([a-zA-Z0-9\._-]+)/([a-zA-Z0-9\._-]+)\.js$ assets/js/min.php?scripts=$1&ver=$2 [L,QSA]
However, now I added a rule to redirect all traffic for nonexistent pages to my index.php file (for MVC routing):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]
RewriteBase /
The problem is, now that I have both of these rules in my HTACCESS file, I am no longer able to access my .css or .js files like:
http://domain.tld/assets/css/min/all/default/0.2.3.css
How can I get the rerouting to index.php to ignore the previous rule with my css and js rewriting?
You can use a negative lookahead in last rule to skip assets/ directory:
RewriteEngine on
RewriteRule ^assets/css/min/([\w.-]+)/([\w.-]+)/([\w.-]+)\.css$ assets/css/min.php?style=$1&theme=$2&ver=$3 [L,QSA]
RewriteRule ^assets/js/min/([\w.-]+)/([\w.-]+)\.js$ assets/js/min.php?scripts=$1&ver=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!assets/)(.+)$ index.php/$1 [L,NC]
Also note that you can replace [a-zA-Z0-9_] with shorter \w
probably my question duplicate with an other title but, I have read too many title and checked many times with google. So if it duplicates, I am sorry.
Now I have an URL:
www.mysite.com/profile.php
I am using this URL rewrite rule:
RewriteRule ^([A-Za-z0-9-_]+)/?$ profile.php?username=$1 [NC,L]
And I can change my url like:
www.mysite.com/username
Then, I need to update my url with an other get parameter:
www.mysite.com/username/photos
At this part, I have an URL like:
www.mysite.com/profile.php?username=xxx&w=photo
I have tried this URL rule for the url for above:
RewriteRule ^([A-Za-z0-9-_]+)/?$/?$ profile.php?username=$1&w=$2 [NC,L]
But It does not work. Please help.
Thank you very much.
-------UPDATE------
Now I can use profile.php which It should be. But other rewrite rules are broken. My current .htaccess file like this:
RewriteEngine On
RewriteRule ^([\w-]+)/([\w-]+)/?$ profile.php?username=$1&w=$2 [QSA,L]
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [QSA,L]
RewriteRule ^p/timeline timeline.php [NC,L]
RewriteRule ^p/notifications notifications.php [NC,L]
First thing first:
[A-Za-z0-9-_]
is not a correct regex because of unescaped hyphen between 9 and _ in a character class that acts as a range between hex 39 and hex 5f. To fix this make sure to use hyphen at first or last position in a character class.
Correct rules will be:
RewriteRule ^([\w-]+)/([\w-]+)/?$ profile.php?username=$1&w=$2 [QSA,L]
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [QSA,L]
Update:
As per your updated question make sure to use generic rule after specific rule. So have your rules like this:
RewriteRule ^p/(notifications|timeline)/?$ $1.php [NC,L]
RewriteRule ^([\w-]+)/([\w-]+)/?$ profile.php?username=$1&w=$2 [QSA,L]
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [QSA,L]
RewriteRule ^([A-Za-z0-9-_]+)/(.+)$ profile.php?username=$1&w=$2 [NC,L]
RewriteRule ^([A-Za-z0-9-_]+)$ profile.php?username=$1 [NC,L]
You can just have one rule if you want. I would also add the conditions so that it makes sure it's not a real directory or file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-_]+)/?([A-Za-z0-9-_]+)?/?$ /profile.php?username=$1&w=$2 [NC,L]
In your PHP you will just have to check the w parameter to see if it's empty or not. If it's not, display the details also.
I've got an issue when I'm trying to add a trailing slash to non existent files. Here is my rewrite rules
# remove www from url
RewriteCond %{HTTP_HOST} ^www.goautohub.com [NC]
RewriteRule ^(.*)$ http://goautohub.com/$1 [L,R=301]
#rewrite news/article name
RewriteRule ^news/([^/]*)/$ news.php?viewnews=$1 [NC,L]
#remove index from url
RewriteRule ^index\.php/?$ / [L,R=301,NC]
#remove php from url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
The only thing left right now I want to do is rewrite this url
/news/mustang-cobra-model-highlights
to
/news/mustang-cobra-model-highlights/
If I use use something like
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
which I found from Force trailing slash at end of rewritten query string it works but it screws up all my other ones it there is already a trailing slash. What it does it adds
/.php/ to the end.
I figure I need a way to limit that to just the news page but I can't seem to get the rule right.
The followin rewrite rule should work:
RewriteRule ^/news/(.*)/$ /news/$1 [NC,L]
I have htaccess file problem. I think my htaccess files conflict.
my .htaccess files:
RewriteEngine on
RewriteRule ^(.*)/video/ video.php?vid=$1 [L]
RewriteRule ^search/(.*)\.html search.php?q=$1 [L]
RewriteRule sitemap-(.*).xml sitemap.php?sayfa=$1 [L]
and my url
Search : http://www.domain.com/search/search_query.html
Video Play : http://www.domain.com/path1/video/video_name.html
.htaccess file conflict video.php and search.php
pls help me
Change order of your rules to keep specific ones first and generic ones later:
RewriteEngine on
RewriteRule ^search/(.*)\.html$ search.php?q=$1 [L]
RewriteRule sitemap-(.*).xml$ sitemap.php?sayfa=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/video/ video.php?vid=$1 [L,NC]
Try using a negative lookahead to prevent the two rules from getting applied to each other:
RewriteEngine on
RewriteRule ^(?!search)(.*)/video/ video.php?vid=$1 [L]
RewriteRule ^search/(?!video)(.*)\.html search.php?q=$1 [L]
Using mod_rewrite, I want to look for all requested files in an assets directory, and otherwise fallback to index.php for API calls. This is my .htaccess.
RewriteEngine on
RewriteRule ^.+\.[^/]+$ assets/$1 [L]
RewriteRule .* index.php
Asset files are determined by whether the last last url segment has a file extension, which means a dot without following slashes. I already tested the regex at regexpal and it seems to work fine.
Unfortunately, all request result in a 500 Internal Server Error using these rewrite rules. I know that mod_rewrite is set up correctly, since omitting the second line works as expected.
Try these rules:
RewriteEngine on
# rewrite to assets/file if file exists in assets dir
RewriteCond %{DOCUMENT_ROOT}/assets/$1 -f
RewriteRule ^(.+?\.[^/]+)/?$ assets/$1 [L]
# otherwise rewrite to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^assets/ index.php [NC,L]
You need to add a condition to prevent the rewrite engine from looping:
RewriteEngine on
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^.+\.[^/]+$ assets/$1 [L]
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule .* index.php
The rewrite engine loops until the URI stops changing, and since .* matches index.php as well, it'll continue to loop through that rule until the internal recursion limit is reached you get a 500 error.
Additionally, you can add conditions that won't reroute existing files or directories:
RewriteEngine on
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^.+\.[^/]+$ assets/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php