apache rewrite rule not working - mod-rewrite

I have the following rewrite rule:
RewriteEngine on
RewriteRule /stylesheets/[0-9]+/(.*)$ /stylesheets/$1 [L]
So in the html I can say /stylesheets/05282011/xxx.css and it points to /stylesheets/xxx.css
This looks correct to me but can anyone spot why it wouldn't work?
Thanks

Problem is starting slash / because that is stripped by Apache. Try this rule:
RewriteRule ^stylesheets/[0-9]+/(.*)$ /stylesheets/$1 [L]

Related

Basic mod_rewrite difficulties

My goal is to rewrite
http://domain.com/subdir/index.php?p=page-title
as
http://domain.com/subdir/page-title
The code below looks right, but I get a 404 Not Found?
RewriteEngine on
RewriteRule ^/subdir/([^/.]+)/?$ /subdir/index.php?p=$1 [L]
Please, remove the leading slash from your rule (assuming it's in .htaccess instead of httpd.conf)
RewriteEngine on
RewriteRule ^subdir/([^/.]+)/?$ /subdir/index.php?p=$1 [NC,QSA,L]

Mod Rewrite Rule

Hi can anybody help me with this rewrite rule.
Basically i have a url http://mydomain.com/http://somewhere.com/photo.gif which i want it to be forwarded to this url: http://mydomain.com/upload.php?url=http://somewhere.com/photo.gif
I have tried the following but it does not work.
RewriteRule ^(.*)$ upload.php?code=$1 [L]
You haven't added a / for upload.php?...
RewriteRule ^(.*)$ /upload.php?code=$1 [L] will work if you can avoid the http:// in http://somewhere.comwhile passing tohttp://mydomain.com/http://somewhere.com/

decoding mod_rewrite charactors

I'm attempting my first pretty url implementation via mod_rewrite. Just want to check if I'm on the right track. I'm doing it via dev environment.
I'm trying to get www.cysticlife.dev/Profile.php?id=34 to become www.cysticlife.dev/34/Profile
Would the regex mod_rewrite version then be:
RewriteEngine on
RewriteRule ^/([0-9]+)/?/Profile$ www.cysticlife.dev/Profile.php?id=$1 [L]
Thanks in advance.
RewriteEngine on
RewriteRule ^/([0-9]+)/Profile/index.html$ /$1/Profile [R=301,L]
RewriteRule ^/([0-9]+)/Profile/$ /$1/Profile [R=301,L]
RewriteRule ^/([0-9]+)/Profile$ www.cysticlife.dev/Profile.php?id=$1 [L]
The "?/" wasn't needed.
The lines I added makes both www.cysticlife.dev/34/Profile, www.cysticlife.dev/34/Profile*/* and www.cysticlife.dev/34/Profile*/index.html* work (with a 301 "Permanently moved" redirection so only one of the three urls is indexed by search engines).
Sidenote: You don't need to specify the full url for your rewrite. You could easily replace the last one with:
RewriteRule ^/([0-9]+)/Profile$ www.cysticlife.dev/Profile.php?id=$1 [L]

Mod_rewrite Help

This is what I have, but it is not working.
RewriteRule ^location/([a-zA-Z0-9_-]+)/reviews$ location.php?purl=$1&page=reviews [L]
RewriteRule ^location/([a-zA-Z0-9_-]+)/reviews/$ location.php?purl=$1&page=reviews [L]
Can anyone show me what I am doing wrong?
http://www.example.com/this-location-1234/reviews/ to http://www.example.com/location.php?purl=this-location-1234&page=review
You should combine those two (nearly identical) rules, and make sure you have the RewriteEngine on and your rule is being run (either your .htaccess is getting picked up, or you have this in an active vhost definition).
RewriteEngine on
RewriteRule ^location/([a-zA-Z0-9_-]+)/reviews/?$ location.php?purl=$1&page=reviews [L]
I fixed it. I accidentally was missing some verbiage in the link I was getting an error on.
RewriteRule ^location/([a-zA-Z0-9_-]+)/reviews/?$ location.php?purl=$1&page=reviews [L]
This works great!
I think this should work for the example URLs you given:
RewriteRule ^([a-zA-Z0-9_-]+)/reviews/?$ location.php?purl=$1&page=reviews [L]
(replace the two rewrite rules with this one)
If you don't already have one, add RewriteEngine on before this RewriteRule.
If you actually wanted to rewrite /location/this-location-1234/reviews/ instead of /this-location-1234/reviews/, then use this:
RewriteRule ^location/([a-zA-Z0-9_-]+)/reviews/?$ location.php?purl=$1&page=reviews [L]
I assume you have another property in your conf file called RewriteEngine on ?
Also, you can turn on the logging with RewriteLogLevel.

mod_rewrite RewriteCond problem

In my .htaccess file I have the following rules:
RewriteEngine on
RewriteBase /myblog.com/
RewriteRule ^author/([^/]*)/?([^/]*)/?$ blog/author.php?author=$1&contactid=$2 [L]
RewriteRule ^blog/([^/]*[^/index.php])/?([^/]*)/?([^/]*)/?$ blog/index.php blog=$1&category=$2&article=$3 [L]
My problem is that a request like this myblog.com/author/Jim+Jones/28 is getting redirected to the following rule, even though I have a [L] flag there?
How can I exclude the second rule from firing when "/author/" appears in the URL?
Many thanks, Jason
/author/Jim+Jones/28 shouldn't get caught by any of these as neither of your rules catch URLs starting with / and the ^ at the start of you regex-es clearly stats that for the first rule it must start with 'author/' (note no / at the start).
UPDATE:
What I think is happening is that requests like myblog.com/author/Jim+Jones/28 are being rewritten twice. First by the first rule and then by the second one. If that's the case you'll need to make the 2nd rule more restrictive. Don't let it rewrite requests that have author.php in them for example.
UPDATE 2:
OK, so what would probably be the best in your case is to add a condition for the 2nd clause:
RewriteEngine on
RewriteBase /myblog.com/
RewriteRule ^author/([^/]*)/?([^/]*)/?$ blog/author.php?author=$1&contactid=$2 [L]
RewriteCond %{THE_REQUEST} !(author\.php)
RewriteRule ^blog/([^/]*[^/index.php])/?([^/]*)/?([^/]*)/?$ blog/index.php?blog=$1&category=$2&article=$3 [L]

Resources