I have a url like this: website.co.uk/search.php?a=category&b=test&c=test
I want to rewrite search.php to /search/ and the variable 'a' only if it exists.
So that the url would redirect to: website.co.uk/search/category/?b=test&c=test
If the url was: website.co.uk/search.php?b=test&c=test
Then this should redirect to website.co.uk/search/?b=test&c=test
Currently I am using:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/(.*?)/?$ search.php?a=$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /search\.php\?a=([^\&\ ]+)
RewriteRule ^/?search\.php$ /search/%1? [L,R=301]
If I enter the url website.co.uk/search.php?a=category&b=test&c=test
It redirects to website.co.uk/search/category
How do I add the remaining parameters to the end of this URL?
So that it looks like website.co.uk/search/category/?b=test&c=test
You can use these rules in your site root .htaccess:
Options -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} /search\.php\?a=([^&\s]*)\sHTTP [NC]
RewriteRule ^ /search/%1/? [L,NE,R=301]
RewriteCond %{THE_REQUEST} /search\.php(?:\?a=([^&\s]*)&(\S+))?\sHTTP [NC]
RewriteRule ^ /search/%1/?%2 [L,NE,R=301]
RewriteCond %{THE_REQUEST} /search\.php(?:\?(\S+))?\sHTTP [NC]
RewriteRule ^ /search/?%1 [L,NE,R=301]
RewriteRule ^search/?$ search.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/([\w-]+)/?$ search.php?a=$1 [L,NC,QSA]
Related
ugly url - example.com/folder_name/art.php?id=9&s=something
want to be - example.com/folder_name/id/s
here is my try
# external
RewriteCond %{THE_REQUEST} \s/+art\.php\?id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ /%1/%2? [R=301,L]
# internal
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ art.php?id=$1&s=$2 [L]
doesn't work
entering ugly url - address bar is unchanged
entering pretty url - getting error 404
pls help
You rules will have to allow folder_name as well so use:
RewriteEngine On
# external
RewriteCond %{THE_REQUEST} \s/+(folder_name)/art\.php\?id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
# internal
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(folder_name)/([\w-]+)/([\w-]+)/?$ $1/art.php?id=$2&s=$3 [L,QSA,NC]
This assumes there is no other .htaccess in folder_name/.
In case there is a .htaccess inside that subfolder then use this .htaccess:
RewriteEngine On
RewriteBase /folder_name/
# external
RewriteCond %{THE_REQUEST} /art\.php\?id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ %1/%2? [R=301,L]
# internal
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ art.php?id=$1&s=$1 [L,QSA]
I've been having trouble with the following rewrite. I'm certain mod_rewrite is enabled but not sure where I am going wrong.
I'm try to change the following pattern:
/profile/?userid=157&username=FirstL
to:
/profile/FirstL
I've tried many different rules, but the two I felt were the closest to being correct aren't working at all. My current failures below:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /profile/+\?userid=$([^&\ ]+)&username=$([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /profile/?userid=$1&username=$2 [L,QSA]
RewriteEngine On
RewriteRule ^([^/]*)$ /profile/?userid=$1&username=$2 [L]
Full htaccess:
Options +FollowSymLinks -Multiviews
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /profile/+\?userid=$([^&\ ]+)&username=$([^&\ ]+)
RewriteRule ^ /profile/%1/%2? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/([^/]+)/([^/]+)$ /profile/?userid=$1&username=$2 [L,QSA]
RewriteBase /
DirectorySlash Off
RewriteRule ^admin$ /admin/index.php [L,E=LOOP:1]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^admin/index.php$ /admin [R=301,L]
# remove .php
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
#Force non-www:
RewriteCond %{HTTP_HOST} www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
You're losing the userID part of the URL, so when you try to internally rewrite that back, there's nothing there:
RewriteRule ^([^/]+)$ /profile/?userid=$1&username=$2 [L,QSA]
This rule says the first match is the "userid" and the second match is the "username", and you only have one match, and on top of that, it doesn't even begin with "profile".
You'll need to include the userid somewhere in the URL, otherwise there's no way to extract it.
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /profile/+\?userid=([^&\ ]+)&username=([^&\ ]+)
RewriteRule ^ /profile/%1/%2? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/([^/]+)/([^/]+)$ /profile/?userid=$1&username=$2 [L,QSA]
I want to rewrite Two URLs in a folder
first is
www.example.com/mybooks/list.php?id=novel-15 to www.example.com/mybooks/novel-15 for this i have the following code in mybooks/.htaccess file [.htaccess is in mybooks folder]
RewriteEngine on
RewriteBase /mybooks/
RewriteCond %{THE_REQUEST} /list\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ list.php?id=$1 [L,QSA]
Its working fine...
Now I want to rewrite
mybooks/search.php?search_word=mystery&search_option=all&page=4 to mybooks/mystery/all/4
i want to do this with out distrub the first rewrite option how can i do this
You can use:
RewriteEngine on
RewriteBase /mybooks/
RewriteCond %{THE_REQUEST} /search\.php\?search_word=([^\s&]+)&search_option=([^\s&]+)&page=([^\s&]+) [NC]
RewriteRule ^ %1/%2/%3? [R=302,L]
RewriteCond %{THE_REQUEST} /list\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/(\w+)/(\d+)/?$ search.php?search_word=$1&search_option=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ list.php?id=$1 [L,QSA]
I have the following problem:
current url:
mysite.com/Category.php?Category=Category name
Want it to show up in the browser as follows:
mysite.com/Category name
I have the following in my .htaccess file
RewriteRule ^([a-zA-Z0-9_-]+)$ Category.php?Category=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ Category.php?Category=$1
but this doesn't seem to do anything
Thanks for looking
Current rules:
rewriteengine on
rewritecond %{HTTP_HOST} ^www.mysite.co.uk$ [OR]
rewritecond %{HTTP_HOST} ^musite1.co.uk$
rewriterule ^(.*)$ "http\:\/\/mysite\.co\.uk\/$1" [R=301,L] #5311a623b538f
rewritecond %{HTTP_HOST} ^www.mysite.co.uk$ [OR]
rewritecond %{HTTP_HOST} ^mysite.co.uk$
rewriterule ^Pianoshop\/detailed_page\.php(.*)$ "http\:\/\/mysite\.co\.uk\/product\.php$1" [R=301,L] #5310700a7e477
rewritecond %{HTTP_HOST} ^www.mysite.co.uk$ [OR]
rewritecond %{HTTP_HOST} ^mysite.co.uk$
rewriterule ^Pianoshop\/index_blog\.php(.*)$ "http\:\/\/mysite\.co\.uk\/news\.php$1" [R=301,L] #53
You need these 2 rules:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+Category\.php\?Category=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ Category.php?Category=$1 [L,QSA]
Do you have mod rewrite enabled ?
Your .htaccess should look like this :
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)/?$ Category.php?Category=$1
I have this code in my, .htaccess file.
My url is fine foodzite.dk/vis/1234567
When i enter www.foodzite.dk/vis/1234567 i want it to redirect to foodzite.dk/vis/1234567
but i redirects to foodzite.dk/shop.php?url=1234567
How is it i cant combine the to Rewrites?
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^vis/([a-z0-9\-_]+)/?$ show.php?url=$1 [L,QSA]
RewriteRule ^postnr/([^/]*)$ postal.php?postal=$1 [NC,L]
RewriteRule ^by/([^/]*)$ city.php?city=$1 [NC,L]
RewriteCond %{HTTP_HOST} !^foodzite\.dk$ [NC]
RewriteRule (.*) http://foodzite.dk/$1 [R=301,L]
You need to have your redirect rule before your routing rule. Otherwise the URI gets routed to your php file, then the redirect rule gets applied and the URI is exposed:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^foodzite\.dk$ [NC]
RewriteRule (.*) http://foodzite.dk/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^vis/([a-z0-9\-_]+)/?$ show.php?url=$1 [L,QSA]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^postnr/([^/]*)$ postal.php?postal=$1 [NC,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^by/([^/]*)$ city.php?city=$1 [NC,L]
Additionally, your conditions only get applied to the immediately following rewrite rule, so the 2nd and 3rd don't have the same conditions. (Unless you wanted it to be that way).
Try something like:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^foodzite.dk [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]