Create RewriteRule using htaccess - .htaccess

I need to create rule in .htaccess to rewrite from http://domain/test to http://domain/?test What should I write to .htaccess?

You can try this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.+)$ /?$1 [L]

Related

.htaccess multiple rewrite rules

I currently have a .htaccess rewrite rule that will redirect all urls containing /ws to the /ws/index.php eg. www.domain.com/ws/controller/function
RewriteEngine on
RewriteBase /ws/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
i'm looking to add another redirect, just the same, for all request that contain /email/ so www.domain.com/email/controller/function will redirect to the /email/index.php.
my full .htaccess looks like the code below but it seems the /email/ never gets called.
RewriteEngine on
RewriteBase /ws/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
RewriteEngine on
RewriteBase /email/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
I've tried adding ^/email to the RewriteRule conditional but to no avail.
Any help would be appreciated.
Thanks
You can use:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^ws/(.*)$ ws/index.php?url=$1 [NC,L,QSA]
RewriteRule ^email/(.*)$ email/index.php?url=$1 [NC,L,QSA]

htaccess redirect if URI contains string to index.php

I would like to redirect link http://example.com/asdasdas/edit to http://example.com/asdasdas/index.php/edit using htaccess. I did this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} edit
RewriteRule ^ %{REQUEST_URI}/index.php [L,R=301]
but it's a loop and doesn't works well. Any ideas?
You can use this rule in /asdasdas/.htaccess:
RewriteEngine On
RewriteBase /asdasdas/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) index.php/$ [L]
This will support URL of http://example.com/asdasdas/edit

Redirect whit htaccess (guion at begin and end)

I need to redirect
http://mysite.com/mp3/-angel-la-pared-shakira-/
to this
http://mysite.com/mp3/angel-la-pared-shakira/
this is my htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^mp3/(.*)/$ search.php?q=$1
RewriteEngine On
RewriteRule (.*)\.xml sitemap.php [nocase]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]
Add this to the beginning of your htaccess file:
RewriteRule ^mp3/-([^/]+)-/$ /mp3/$1/ [L,R=301]

.htaccess single variable url rewrite

I am trying to redirect http://test.pearlsquirrel.com/explore_all?u=hiphop to http://test.pearlsquirrel.com/explore_all/hiphop using htaccess. However, It keeps redirecting here: http://test.pearlsquirrel.com/explore_all/?u=site_error.php. I have looked all over Stackoverflow for the answer but I just can't seem to find it. I would really appreciate some help in fixing this problem, thanks!
Here is my .htaccess file
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.pearlsquirrel.com$ [NC]
RewriteRule ^(.*)$ http://pearlsquirrel.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) explore_all?u=$1 [L]
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} [^=]+=([^=]+)
RewriteRule .* /%1? [L]
Will map this:
http://test.pearlsquirrel.com/explore_all?u=hiphop
To this:
http://test.pearlsquirrel.com/explore_all/hiphop
Replace the corresponding code in your .htaccess file with this one.
I have not checked the rules in your .htaccess file.
OPTION
Now, if it is the other way around, which I think it is, and the idea is to map this incoming URL:
http://test.pearlsquirrel.com/explore_all/hiphop
To this resource:
http://test.pearlsquirrel.com/explore_all?u=hiphop
Replace the above rule set with this one:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .*/([^/]+)/?
RewriteRule .* http://test.pearlsquirrel.com/explore_all?u=%1 [L]

Simple .htaccess rewrite to pass 1 parameter

Can someone please tell me what should my .htaccess file contain to create the following rewrite:
http://www.example.com/success
to call
http://www.example.com/index.php?q=success
Try with:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?q=$1 [L]
Following code I use for my site
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?q=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?q=$1
or you can write following
RewriteEngine On
RewriteRule ^(.+)$ index.php?q=$1
RewriteRule ^(.+)/$ index.php?q=$1

Resources