RewriteRule question - mod-rewrite

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]

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]

Rewrite Rule not taking effect

I am trying to rewrite some URLs using my .htaccess file and the following syntax:
RewriteEngine On
RewriteRule ^movie/([0-9]+)/$ movie.php?id=$1
Basically, the URL http://screeningapp.co.uk/movie.php?id=771316320 should be rewritten to http://screeningapp.co.uk/movie/771316320, but that's not happening and I'm not sure why.
Thanks!
RewriteEngine On
RewriteRule ^movie/([0-9]+)/?$ /movie.php?id=$1 [L,nc]
#if you wanna redirect movie.php?id=1 to movie/1/
RewriteCond %{QUERY_STRING} ^id=([0-9]+)($|&)
RewriteRule ^movie.php$ /movie/%1? [R=301,L,NC]

.htaccess redirect all .html pages to process.php with get parameter

I want to redirect all links that have .html extension to a process.php file with directory + page name as get parameter. I have tried these link1 link2 solution but it does not work.
for e.g
http://localhost/Site/directory1/test1.html
http://localhost/Site/directory2/test2.html
to redirected to process.php as
http://localhost/Site/process.php?directory1/test1.html
http://localhost/Site/process.php?directory2/test2.html
I have tried like this.
RewriteEngine on
RewriteRule ^/(.*).html /process.php?page=$1
and this
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*/)?([^/]*)\.php$ /process.php?page=$2 [R=301,L]
but it does not work.
Please see and suggest any possible way to do this.
Try this:
RewriteEngine on
RewriteRule ^([^/]*)/(.*\.html) /$1/process.php?page=$2 [R=301,L]
Your second example is very close to what I believe your looking for. Your just off by one directory and your looking for .php when you should be looking for .html. Give this a try.
RewriteEngine on
RewriteBase /
RewriteRule ^/(.*)/(.*)/(.*)\.html$ /Site/process.php?$2/$3 [R=301, L]
This should do(assuming that the .htaccess file lives in the Site directory):
RewriteEngine On
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*?/)?(.*)$ /$1/process.php?page=$2 [R=301,L]

Domain Url Rewrite

What I'm looking to do is rewrite the url so that one of the directories are hidden. For example;
http://www.example.com/home/example.html
to
http://www.example.com/example.html
As I'm new to .htaccess and mod_rewrite, is this something that can be done?
This should work:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/home/.*$
RewriteRule ^(.*)$ /home/$1 [L]
Something like this should work.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^example.html$ /home/example.html [NC,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.

Resources