I'm trying to rewrite a url of this:
http://www.foo.com/bar/baz
to
index.php?q=$1&d=baz
Where bar is not a fixed value, but baz is.
RewriteRule ^(.*)\/baz$ index.php?q=$1&d=baz [L,QSA]
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
What i have above kinda works but unfortunately breaks all the includes in the site (css/javascript) but strangely all the pages work :/
This is a drupal install, (so the second line needs to remain).
UPDATE
This might help actually, i forgot to include
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
**RewriteRule ^(.*)/details index.php?q=$1&details=true [L,QSA]**
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
It seems to be doing my rewrite correctly, the only problem is it's ignoring the other conditional statements now, i.e. it's still attempting to rewrite files that exist (i.e. css,js) when it's mean to avoid them.
site is fine without my line (The one with the stars), but with it, the variables and pages work, but static files like css etc are also being rewritten....need to stop that!
Thanks in advance.
Shadi
Conditions only apply to the first rule immediately following. So try duplicating the condition lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/details index.php?q=$1&details=true [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
RewriteRule ^(.*)/details index.php?q=$1&details=true [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
This is what finally worked. it just required the rearranging of the rules...
Related
I have to rewrite rules that collide with each other. Actually, it's the second rule that causes the first not to work, even when I switch positions.
artistmusic.php
From: http://www.example.com/artistmusic?slug=Ben
To: http://www.example.com/Ben/music
artistvideos.php
From: http://www.example.com/artistvideos?slug=Ben
To: http://www.example.com/Ben/videos
Rewrite rule for the above urls.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)$ /artist$2?address=$1 [L]
This works like it should, until I add the rewrite rule for the set of URls below.
allsongs.php
From: http://www.example.com/allsongs?standard=popular&request=monthly
To: http://www.example.com/songs/popular/monthly
allartists.php
From: http://www.example.com/allartists?standard=popular&request=monthly
To: http://www.example.com/artists/popular/monthly
Rewrite rule for the second set above.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /all$1?standard=$2&request=$3
The rule above works but kills the first one.
What is the cause of this? Thanks.
With your shown samples/attempts, please have your htaccess rules file in following manner. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^songs/([^/]*)/([^/]*)/?$ allsongs.php?standard=$1&request=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^artists/([^/]*)/([^/]*)/?$ allartists.php?standard=$1&request=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(music|videos)/?$ artist$2.php?slug=$1 [QSA,NC,L]
Generic solution: In case you don't want to hard code values in your new rules then try following. Make sure use either above OR following only 1 at a time.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ all$1.php?standard=$2&request=$3 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ artist$2.php?slug=$1 [QSA,NC,L]
I have the following problem. I have a domain with SEO pages, generated in html each night. This URLs to the pages could look like this:
https://www.mypages.com/seo/florida/miami/somesuburb
but it's possible that "somesuburb" doesn't exist anymore. So it could be, that:
https://www.mypages.com/seo/florida/miami/somesuburb leads to a 404.
also
https://www.mypages.com/seo/florida/miami could lead to a 404.
So I tried to do this in my htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (^(.*[\\\/])) /seo/$1 [L,R=301]
this works, for example:
https://www.mypages.com/seo/florida/miami/doesntexist
goes to:
https://www.mypages.com/seo/florida/
but if the Dir florida also does not exist, it breaks and gives a 404. I think it is because of the trailing slash.
so my next approach was:
RewriteRule ^(.*)/+$ /seo/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (^(.*[\\\/])) /seo/$1 [L,R]
This leads into a loop :(
Could anyone help out here?
Greets!
found a solution:
RewriteEngine on
RewriteCond %{REQUEST_URI} /+[^\.]+$/
RewriteRule ^(.+[^/])$ %{REQUEST_URI} [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /seo/$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (^(.*[\\\/])) /seo/$1 [L,R]
this goes back as long as a dir is valid
I have a folder structure like this
/localhost/parent/
I have an .htaccess residing in
/localhost/parent/
I want to rewrite everything from /localhost/parent/WebContent/ to /localhost/parent/
here is my code
RewriteEngine on
RewriteRule ^$ WebContent/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^WebContent/
RewriteRule ^(.*)$ WebContent/$1
For some reason the first two lines work fine, but the rest doesn't work. So I am able to redirect only homepage.
Update
RewriteBase /parent/
RewriteEngine on
RewriteRule ^$ WebContent/$1
#RewriteCond %{REQUEST_URI} !WebContent
RewriteRule (.*) /parent/WebContent/$1 [R=301,L,QSA]
Goes into an infinite loop. I just need to avoid that loop now
Try switching the order of the lines so that
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
comes before everything else.
You could also try adding this line:
RewriteBase /WebContent/
I used this
RewriteEngine on
RewriteRule ^$ /parent/WebContent/$1
RewriteCond %{REQUEST_URI} !WebContent
RewriteRule ^(.*)$ /parent/WebContent/$1
How do I go about writing the conditions for mod_rewrite to make this example happen?
Have a URL this like...
http://domain.com/recent/5
Render this page...
http://domain.com/index.php?view=recent&page=5
And if there is no page number specified (note I want it to work with or without trailing-slash)...
http://domain.com/recent/
It will default to page 1...
http://domain.com/index.php?view=recent&page=1
But when someone just goes to the root domain...
http://domain.com
I want this page to render...
http://domain.com/index.php?view=popular&page=1
Thanks for any help.
Put this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([0-9]+) /index.php?view=$1&page=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?view=$1 [L,QSA]
RewriteRule ^$ /index.php?view=popular&page=1 [L,QSA]
What I'm trying to achieve:
1) http://localhost/en/script.php?param1=random is mapped to http://localhost/script.php?param1=random&language=English
This has to work always.
2) http://localhost/en/random/text/here will be mapped to http://localhost/categories.php?term=random/text/here
This has to work if random/text/here is 404
What I have at the moment:
RewriteEngine on
RewriteCond substr(%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en/(.+)$ categories.php?lang=English&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ee/(.+)$ categories.php?lang=Estonian&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fi/(.+)$ categories.php?lang=Finnish&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ru/(.+)$ categories.php?lang=Russian&terms=$1 [L]
RewriteRule ^en/(.*) $1?lang=English [QSA]
RewriteRule ^ee/(.*) $1?lang=Estonian [QSA]
RewriteRule ^ru/(.*) $1?lang=Russian [QSA]
RewriteRule ^fi/(.*) $1?lang=Finnish [QSA]
What is the problem:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
It's supposed to redirect to categories.php?lang=English IF /en/this/here/does/not/match/a/script. If I load an URL like en/index.php it will also get mapped to categories.php?lang=English because en/index.php does not exist.
What I've thought:
substr(%{REQUEST_FILENAME},3) would fix my problem (as currently /ee/index.php is literally mapped to /ee/index.php instead of just /index.php)
Unfortunately I couldn't find a way to manipulate strings :/
I take it the language code is what makes the URL map to a non-existant file. Switch the two steps, moving the language code to the query string first. This also has the added advantage of simplifying the keyword step to a single RewriteRule, since they no longer need to do two things at once.
RewriteRule ^en/(.*) $1?lang=English [QSA,DPI]
RewriteRule ^ee/(.*) $1?lang=Estonian [QSA,DPI]
RewriteRule ^ru/(.*) $1?lang=Russian [QSA,DPI]
RewriteRule ^fi/(.*) $1?lang=Finnish [QSA,DPI]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ categories.php?terms=$1 [L,QSA]
the issue is that you are using the L flag. Which means that rule will be the last to be executed.
also
%{QUERY_STRING}
isnt necessary, add QSA and you will get all parameters added to the end of the url
try to do:
RewriteRule ^en/(.*) $1?lang=English [QSA]
For substr problem, you could try absolute paths:
RewriteRule ^en/(.*) /$1?lang=English&%{QUERY_STRING}
or
RewriteRule ^en/(.*) http:/localhost/$1?lang=English&%{QUERY_STRING}
Also, I might be nitpicking, but wouldn't it be easier if you did the language evaluation in the PHP base on language codes, 404'ed non-existent languages and used
RewriteRule ^(.*?)/(.*) $2?lang=$1&%{QUERY_STRING}
Edit: depending on how many scripts you have, can't you do something like:
RewriteRule ^(.*?)/(script.php|other.php) $2?lang=$1 [QSA]
= have pipe-list files, that are accesible?