Mod_rewrite Help - mod-rewrite

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.

Related

CodeIgniter and specific rewrite rule

On my CodeIgniter site, I would like to add a specific rewrite rule, so that this url
http://www.exemple.com/cache.manifest
would rewrite to
http://www.exemple.com/controller/manifest
(because Safari 7 seems to only accept .manifest files for ApplicationCache)
So I try to add this line to my htaccess
RewriteRule ^cache.manifest$ controller/manifest
I added it before the other rewrite rules :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^cache.manifest$ controller/manifest
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
But it returns a 404. If I change the line to
RewriteRule ^cache.manifest$ test.html
it works. So the first part on my rule is correct.
If I try to access directly to www.example.com/controller/manifest, it works to, so my url is correct.
I tried also
RewriteRule ^cache.manifest$ index.php/controller/manifest [L]
But it doesn't work either…
Any clue ?
Thanks a lot
I tried some tests on my local server and I think the following might work:
RewriteRule ^cache.manifest$ /index.php/controller/manifest [R,L]
I am not entirely sure if you need the leading "/" or "/index.php", so you
may need to experiment.
You need the [R] flag to force a redirect. In this situation, you want Apache
to look for the string cache.manifest in the URL, and then go to the CI page
controller/manifest.
It appears that you need to explicitly set the redirect.
Please let me know if this works. Good luck!

How To Rewriting URLs with Mod-Rewrite

How would I rewrite:
http://mydomain.com/?v=service
http://mydomain.com/?v=pfolio
To
http://mydomain.com/service
http://mydomain.com/pfolio
I am have tried many ways but have not figured it out. Please give a suggestion.
I already use
RewriteRule ^/?v=(.*)$ /v/$1 [R=301,L]
This would be the version for the server configuration:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^v=(.*)$
RewriteRule ^/$ /%1 [R=301,L]
Or, if there might occur more query parameters something like this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^v=([^&]*)
RewriteRule ^/$ /%1 [R=301,L,QSA]
When placed inside a .htaccess style file you have to remove the / from the pattern in the RewriteRule (so just ^$).
But I am skeptical what this really is what you are really looking for. Usually people want to do the opposite of this... Maybe you should also explain what is the idea behind your attempt. So your situation, what you want to happen, not how.

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: How to match file in directory or in root depending on 'availability'?

Hello, long time listener, first time caller here!
Thank you for the excellent advice you all share.
I have these mod_rewrite rules set up:
RewriteRule ^([^/.]+)/([^/.]+)/?$ $1--$2.php?%{QUERY_STRING} [L]
RewriteRule ^([^/.]+)/?$ $1.php?%{QUERY_STRING} [L]
They make /company/services/ redirect to company--services.php. Or /company/ to company.php. Works perfect.
But now I'd like to another rule that if I were to put services.php inside physical directory /company/ it will match and redirect that. And if failing that, look for my initial rule. (And failing that, return 404.)
I figured it would be as simple as including:
RewriteRule ^([^/.]+)/([^/.]+)/?$ $1/$2.php?%{QUERY_STRING} [L]
but not so. It will returns a 404 instead. I'm a bit stumped as this goes against how I believed mod_rewrite to work (if a rule does not match, go to the next one.)
Thank you for any pointers!
Found it. For reference, this is the complete set of rules:
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/.]+)/([^/.]+)/?$ /$1/$2.php?%{QUERY_STRING} [L]
RewriteRule ^([^/.]+)/([^/.]+)/?$ $1--$2.php?%{QUERY_STRING} [L]
RewriteRule ^([^/.]+)/?$ $1.php?%{QUERY_STRING} [L]

RewriteRule question

Is there any way i can use RewriteRule to show these links:
/articles_history.php
/articles_geography.php
as
/articles/history.html
/articles/geography.html
I created a .htacesss file on my root directory and would like to know if the above is possible by placing some kind of code in the .htaccess file.
RewriteEngine On
RewriteRule /articles/(.+)\.html /articles_$1.php [L,QSA]
Yes it is.
See http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
In addition to RewriteRule, you also need to usually turn the rewrite engine on by the RewriteEngine directive.
Try this in your .htaccess:
RewriteEngine on
RewriteRule ^article/([^/]+)\.html$ articles_$1.php [L]
And for an arbitrary input:
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)\.html$ $1_$2.php [L]

Resources