I have form with two parameters city,keyword which are passed to search.php ,after submitting it redirects to url like
website.com/search/lenovo+laptop+dealers/Delhi
But i need url to be parsed without those plus signs but with a dash in url
Example: website.com/search/lenovo-laptop-dealers/Delhi
Code in my htaccess file
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+search\.php\?keyword=([^&]+)&city=([^\s&]+) [NC]
RewriteRule ^ /search/%1/%2? [R=301,L,NE]
RewriteRule ^search/([\w-]+)/([\w-]+)/?$ /search.php?keyword=$1&city=$2 [L,QSA]
You can have your rules like this:
RewriteEngine on
RewriteBase /
RewriteRule "^(search)/([^+]*)\++([^+]*\+.*)$" /$1/$2-$3 [L,NC]
RewriteRule "^(search)/([^+]*)\+([^+]*)$" /$1/$2-$3 [L,R=301,NE,NC]
RewriteCond %{THE_REQUEST} \s/+search\.php\?keyword=([^&]+)&city=([^\s&]+) [NC]
RewriteRule ^ /search/%1/%2? [R=301,L,NE]
RewriteRule ^search/([\w-]+)/([\w-]+)/?$ /search.php?keyword=$1&city=$2 [L,QSA]
Related
I am facing a couple of 301 redirect issues.
Issue 1:
I want to redirect some links to another URL of the same site:
Redirect 301 /path-example.html /dir/subdir/
The redirect is working but it's generating query string like this:
example.com/dir/subdir/?str=path-example
How do I remove the query sting ?str=path-example ?
Issue 2:
I want to redirect some links to the homepage:
Redirect 301 /some-path.html /
But it's leaving a ? after a trailing slash like this:
example.com/?
How do I remove /? from the forwarded URL?
Existing rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{QUERY_STRING} ^s=(.*)$
RewriteRule ^$ / [R=301,L]
Thanks!
Please place these rules top of your htaccess files(in case you have more rules in it). Please make sure to clear your browser cache before testing your URLs.
<IfModule mod_rewrite.c>
RewriteEngine ON
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^index\.php/?$ - [L,NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
##Redirection with removing query string rule here.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\?\S+\s [NC]
RewriteRule ^ /dir/subdir? [R=301,L]
##Redirection to site's home page here for html urls.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\s [NC]
RewriteRule ^ /? [QSD,R=301,L]
RewriteCond %{QUERY_STRING} ^s [NC]
RewriteRule ^/?$ / [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
EDIT: My first answer without OP's other rules set is as follows:
RewriteEngine ON
##Redirection with removing query string rule here.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\?\S+\s [NC]
RewriteRule ^ /dir/subdir? [R=301,L]
##Redirection to site's home page here for html urls.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\s [NC]
RewriteRule ^ /? [QSD,R=301,L]
I want to change "+" to "-" in URL in php application by using htaccess
Example
http://hddir.com/videos/kodak+black
To
http://hddir.com/videos/kodak-black
My htaccess code
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]
RewriteRule ^videos/(.+)/(.+)/(.+)/(.+) search.php?q=$1&order=$2&type=$3&ptk=$4
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+) [NC]
RewriteRule ^ videos/%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteRule ^videos/(.+)$ search.php?q=$1 [L,QSA,NC]
RewriteRule ^video/(.+).html$ watch.php?v=$1
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
#######Options -Indexes
ErrorDocument 404 /index.php
The following should do the trick by redirecting every domain with a plus in it permanently to the same domain but a minus instead of plus:
RewriteEngine On
RewriteRule ^(.*)\+(.*)$ /$1-$2 [L,R=301]
I have changed my web page url from localhost/products.php to localhost/products using the code below.
RewriteCond %{THE_REQUEST} /products\.php[\s/?] [NC]
RewriteRule ^ /products [R=301,L]
RewriteRule ^products/?$ products.php [L,NC]
But when I navigate to next page, address bar shows something like:
localhost/products?page=2 which I want to appear as localhost/products/page2
Tried this RewriteRule ^products/page/([0-9]+)/?$ products.php?page=$1 [L] but this did not work.
My full code for rewriting page url is:
RewriteCond %{THE_REQUEST} /products\.php[\s/?] [NC]
RewriteRule ^ /products [R=301,L]
RewriteRule ^products/?$ products.php [L,NC]
RewriteRule ^products/page/([0-9]+)/?$ products.php?page=$1 [L]
Here how can I change the URL for next pages? I mean from localhost/products?page=2 to localhost/products/page2 and so on.
You can use the following rule :
RewriteCond %{THE_REQUEST} /products/?(?:\.php)?\?page=([^\s&]+) [NC]
RewriteRule ^ /products/%1? [R=301,L]
RewriteRule ^products/([0-9]+)/?$ /products.php?page=$1 [L,NC]
This will convert the urls
/products.php?page=number
/products/?page=number
to
/products/number
The site I am trying to work on is http://northidahocvma.org/2GD/index.php?page=1
So suppose I have URLs with query string parameters like these:
/index.php?page=1
Using mod_rewrite, how can I redirect them to URLs like these?
/page/1 or /1/
I was given this code but still cant quite figure it out
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=302,L]
RewriteRule ^page/([^/.]+)/?$ /index.php?page=$1 [L,QSA,NC]
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=302,L]
RewriteRule ^page/([^/.]+)/?$ /index.php?page=$1 [L,QSA,NC]
I am attempting to get the following results with an .htaccess file.
URL: Behavior(rewrite to):
example.com/x/ view-test.php?page=x
example.com/x/y view-test.php?page=y&parent=x
example.com/x/y/z view-test.php?page=z&parent=y&grandparent=z
I have the following .htaccess rules, which also include some folders which needs to ignore this scheme:
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]*)/([^/]+)/?$ /view-test.php?grandparent=$1&parent=$2&page=$3 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]+)/?$ /view-test.php?&parent=$1&page=$2 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
#RewriteRule ([^/]+)$ /view-test.php?page=$1 [L]
however, whenever I uncomment the last two lines I get an internal server error. the parent/page and grandparent/parent/page structures work fine. How can I re-write this last rule (and the preceding rules if nessecary) to achieve the desired result for the first pattern (just a page)?
# remove slash at the end to prevent redirect loop
RewriteCond %{REQUEST_URI} ^/(OIE|admin|images|scripts|css|fonts|view-test\.php) [NC]
RewriteRule ^ - [L]
RewriteRule ([^/]*)/([^/]*)/([^/]+)/?$ /OIE/view-test.php?grandparent=$1&parent=$2&page=$3 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]+)/?$ /OIE/view-test.php?&parent=$1&page=$2 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]+)/?$ /OIE/view-test.php?page=$1 [L]
Use this code instead:
RewriteCond %{REQUEST_URI} ^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ^ - [L,S=3]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ view-test.php?grandparent=$1&parent=$2&page=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ view-test.php?&parent=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ view-test.php?page=$1 [L,QSA]
Problem was that you were missing trailing slash / in in your last RewriteRule condition.
Try this :
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ view-test.php?page=$1
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ view-test.php?page=$1&parent=$2
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ view-test.php?page=$1&parent=$2&grandparent=$3