Two level url rewrite with mod_rewrite - apache

I need something like this index.php?page=home to be /home
and
index.php?page=products&cat=chairs to be /products/chairs
What would be the mod_rewrite rule to achieve this?
Edited
I have other pages than home, so my full content of .htaccess file is now
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^/products(:/([^/]+))? /index.php?page=products&cat=$1 [L]
But when I navigate to /products/chairs I get a "Object not found" error.

RewriteEngine on
RewriteRule ^/home/?$ /index.php?page=home [L]
RewriteRule ^/products(:/([^/]+))? /index.php?page=products&cat=$1 [L]
It rewrites both /home and /home/, both /products and /products/ (you didnt specify which was your intended behavior); beware that you will get an empty cat parameter in those cases.
EDIT: this is a tested .htaccess, with R=301 left on it to see rewriting result, with both .htaccess and index.php in same directory (webroot in my case):
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [R=301,L]
RewriteRule ^products/([^/]+)?/? index.php?page=products&cat=$1 [R=301,L]

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=(products)&cat=([^\s]+) [NC]
RewriteRule ^ %1/%2 [R=302,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=(home)\s [NC]
RewriteRule ^ %1 [R=302,L]
RewriteRule ^(products)/([^/]+)/?$ index.php?page=(products)&cat=$1 [L,QSA,NC]
RewriteRule ^(home)/?$ index.php?page=$1 [L,QSA,NC]
Change R=302 to R=301 after you verify it's working fine for you.

Related

apache rewritemap not working URL does not translate

I cannot get my rewrite map to work. I have read a lot of pages on this and yet can't figure out how.
I have the following in my httpd.conf and took an apache restart
RewriteMap ErrorMap txt:/usr/local/apache/conf/userdata/std/1/techencl/site.com/vb2xf.map
The rewrite map has the following entry (edited after the tip in the first reply)
corner/lock-unlock-140859.html http://www.site.com/com/ts/lock-unlock.70898/
The .htaccess in the root directory has the following entry
RewriteCond ${ErrorMap:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*)$ ${ErrorMap:$1|$1} [R=301]
Edit: Just posting my current .htaccess file contents for reference
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule [^/]+/.+-([\d]+)/p([\d]+)/ showthread.php?t=$1&page=$2 [NC,L]
RewriteRule [^/]+/.+-([\d]+)/ showthread.php?t=$1 [NC,L]
Options +FollowSymlinks
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
RewriteCond ${ErrorMap:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*)$ ${ErrorMap:$1|$1} [R=301]
</IfModule>
When you call the map:
${ErrorMap:$1
The $1 is never going to have http://www.site.com/ in it. It will only have this part:
corner/lock-unlock-140859.html
That means in your map, you need to make it look like this:
corner/lock-unlock-140859.html http://www.site.com/com/ts/lock-unlock.70898/

Stop Apache mod_rewrite affecting Redirect rules

I have the following .htaccess file:
Redirect 301 /test/example-1 /test/example-2
RewriteEngine On
RewriteBase /test/
RewriteRule ^(.*)$ index.php?_url=$1 [QSA,L]
I want the redirect to happen without mod_rewrite interfering with it, but at the moment when you hit /test/example-1 it redirects to /test/example-2?_url=example-1.
Is there any way to stop the query parameter from the rewrite rule getting append to the redirect URL? Thanks.
Have your rewrite rule as:
RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_URI} !/(example-1|index\.php) [NC]
RewriteRule ^(.*)$ index.php?_url=$1 [QSA,L]
But in general you shouldn't mix mod_alias rules with mod_rewrite rules. Your code can be rewritten as:
RewriteEngine On
RewriteBase /test/
RewriteCond %{THE_REQUEST} /example-1[\s?/] [NC]
RewriteRule ^ example-2 [R=301,L]
RewriteRule ^(.*)$ index.php?_url=$1 [QSA,L]

.htaccess - won't add www to only one subdirectory

I have a .htaccess in root directory, with this content:
<IfModule mod_rewrite.c>
RewriteEngine on
#RewriteCond %{HTTP_HOST} ^domain.com
#RewriteRule (.*) http://www.domain.com$1 [R=301,L]
</IfModule>
If I go to http://domain.com/subdir it rewrites to http://www.domain.com/subdir.
But I have a problem with one subdirectory named "crm" - if I go to http://domain.com/crm it redirects me to http://www.domain.com instead of http://www.domain.com/crm.
In this "crm" subdirectory I have another .htaccess file, which rewrites .php extension to .html. Here is the code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([^/]*)/([^/]*)\.html$ ?name=$1&id=$2 [L]
RewriteRule ^(.*).html$ index.php?name=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/([^/]*)\.html$ ?name=$1&id=$2 [L]
</IfModule>
Can someone please tell me how can I make this work? So when I will go to http://domain.com/crm it will redirect me to http://www.domain.com/crm.
EDIT:
Root .htaccess was ok, I was changing something and forgot to remove comments for stackoverflow.
crm/.htaccess is now:
RewriteEngine on
RewriteOptions Inherit
RewriteRule ^([^/]*)/([^/]*)\.html$ ?name=$1&id=$2 [L]
RewriteRule ^(.*).html$ index.php?name=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/([^/]*)\.html$ ?name=$1&id=$2 [L]
But it still doesn't redirect this subdir to www.domain.com/crm but only to www.domain.com :(
First change root .htaccess to this: (seems to be commented at present)
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Then in crm/.htacess add this line just below RewriteEngine on:
RewriteOptions Inherit
UPDATE: Your crm/.htaccess is faulty. Replace that content with code from below:
RewriteEngine on
RewriteOptions Inherit
RewriteRule ^([^/]*)/([^/]*)\.html$ ?name=$1&id=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)\.html$ index.php?name=$1 [QSA,L]
Once this code is there redirect will happen as expected.

.htaccess RewriteRule redirect

i need help redirecting this two links to one link:
Link #1: http://www.domain.com/index.php?til=d_news&id_new=1
Link #2: http://www.domain.com/new_folder/?til=d_news&id_new=1
need to redirect to:
http://www.domain.com/news/news.html
i tried using this code:
RewriteCond %{QUERY_STRING} ^til=d_news
RewriteRule ^(.*)$ http://%{HTTP_HOST}/news/news.html? [R=301,L]
but it only redirects the first link and not the second.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^til=d_news&id_new=1 [NC]
RewriteRule ^ /news/news.html? [R=301,L]
In the htaccess file in your document root, add:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^til=d_news&id_new=1
RewriteRule ^index.php$ /news/news.html? [L,R=301]
RewriteCond %{QUERY_STRING} ^til=d_news&id_new=1
RewriteRule ^new_folder/?$ /news/news.html? [L,R=301]
In your second link there is no destination page:
http://www.domain.com/new_folder/?til=d_news&id_new=1
should be something like:
http://www.domain.com/new_folder/page.php?til=d_news&id_new=1

Mod Rewrite problem

.htacces
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ sinj.com.hr/index.php?var1=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ sinj.com.hr/index.php?var1=$1&var2=$2 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ sinj.com.hr/index.php?var1=$1&var2=$2&var3=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ sinj.com.hr/index.php?var1=$1&var2=$2&var3=$3&var4=$4 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/$ sinj.com.hr/$1 [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ sinj.com.hr/$1/$2 [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ sinj.com.hr/$1/$2/$3 [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ sinj.com.hr/$1/$2/$3/$4 [R=301,L]
There is folder sinj.com.hr/administracija and when I try to access http://localhost/sinj.com.hr/administracija I am redirected to http://localhost/sinj.com.hr/administracija?var1=administracija
What I would like is when user enters http://localhost/sinj.com.hr/administracija that he is redirected to http://localhost/sinj.com.hr/administracija/index.php. I tried to do this with header("Location:... ") but it always redirects me to http://localhost/sinj.com.hr/administracija?var1=administracija. If folder administracija is renamed then header() function works. Any ideas how to solve this?
Thanks,
Ile
Try this rule to test if the request can be mapped to a directory that contains an index.php file:
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^ %{REQUEST_URI}/index.php
Additionally you can use this single rule to redirect any requests with a URL path that ends with a slash the one without:
RewriteRule ^(.+)/$ sinj.com.hr/$1 [R=301,L]