rewrite url's without the second variable - .htaccess

I have a url that can be any of the below:
1. example.com/index/var1=word&var2=word&var3=number&var4=number&var5=word&var6=word&page=number
2. example.com/index/var1=word&var2=word&var3=number&var4=number&var5=word&var6=word
3. example.com/index/var1=word&var3=number&var4=number&var5=word&var6=word&page=number
4. example.com/index/var1=word&var3=number&var4=number&var5=word&var6=word
This is my current htaccess:
RewriteCond %{THE_REQUEST} /index\?var1=([^&\s]+)&var2=([^&\s]+)&var3=([^&\s]+)&var4=([^&\s]+)&var5=([^&\s]+)&var6=([^&\s]+) [NC]
RewriteRule ^ index/%1/%2/%3/%4/%5/%6? [L,R=302,NE]
RewriteRule ^index/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?var1=$1&var2=$2&var3=$3&var4=$4&var5=$5&var6=$6&page=$7 [L,QSA]
RewriteRule ^index/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?var1=$1&var2=$2&var3=$3&var4=$4&var5=$5&var6=$6 [L,QSA]
RewriteRule ^index/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?var1=$1&var3=$2&var4=$3&var5=$4&var6=$5 [L,QSA]
URL's 1,2 and 4 are working, however URL number 3 is being read as:
3. RewriteRule ^index/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?var2=$1&var3=$2&var4=$3&var5=$4&var6=$5 [L,QSA]
What needs to be changed?

You can match the 2nd perameter optionally in your condition and Rule's pattern :
RewriteCond %{THE_REQUEST} /index\?var1=([^&\s]+)(?:&var2=([^&\s]*/?))?&var3=([^&\s]+)&var4=([^&\s]+)&var5=([^&\s]+)&var6=([^&\s]+) [NC]
RewriteRule ^ index/%1/%2%3/%4/%5/%6? [L,R=302,NE]
RewriteRule ^index/([^/]+)/([^/]*/)?([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?var1=$1&var2=$2&var3=$3&var4=$4&var5=$5&var6=$6&page=$7 [L,QSA]

Related

How to rewrite paged URL's?

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

(htaccess) Pagination, if the npage parameter is equal to 1 redirect to relative base url

I have 3 parameters in URL:
?lang
&page
&npage
If the third parameter, npage, has the value 1, i need to redirect to relative base URL.
From URL
\en\news\npage-1\
I want to redirect to
\en\news\
My htaccess code is this:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# first parameter (lang)
RewriteRule ^([^/]+)/$ index.php?lang=$1 [L,QSA]
# second parameter (page)
RewriteRule ^([^/]+)/([^/]+)/$ index.php?lang=$1&page=$2 [L,QSA]
# third parameter (npage) with pagination
RewriteRule ^([^/]+)/([^/]+)/npage-(\d+)/$ index.php?lang=$1&page=$2&npage=$3 [L,QSA]
# if first page, redirect to relative base url
RewriteCond %{QUERY_STRING} (^|&)npage=1(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
Try:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# first parameter (lang)
RewriteRule ^([^/]+)/$ index.php?lang=$1 [L,QSA]
# second parameter (page)
RewriteRule ^([^/]+)/([^/]+)/$ index.php?lang=$1&page=$2 [L,QSA]
# third parameter (npage) with pagination
RewriteRule ^([^/]+)/([^/]+)/npage-(\d+)/$ index.php?lang=$1&page=$2&npage=$3 [QSA]
# if first page, redirect to relative base url
RewriteCond %{QUERY_STRING} (^|&)npage=1(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
Without [L] flag
Thanks.
Your code works if it is the last line in .htaccess, but if there are more lines after it, it doesn't work well.
In alternative, I'm trying this code and it works perfectly:
# third parameter (npage) with pagination
# (if the parameter npage has value 0 or 1, redirect to relative base url)
RewriteRule ^([^/]+)/([^/]+)/npage-([0-1]+)/$ $1/$2/ [R=301,L]
# third parameter (npage) with pagination
RewriteRule ^([^/]+)/([^/]+)/npage-(\d+)/$ index.php?lang=$1&page=$2&page=$3 [L,QSA]
And removing the old lines:
# if first page, redirect to relative base url
RewriteCond %{QUERY_STRING} (^|&)npage=1(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]

htaccess search and replace plus sign with dash

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]

making url rewrites dynamic

Here is an example of my url rewrites.
# Redirect /page1.php?parameter-view=value to /page1/value
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page1\.php\?parameter-view=([^\s]+) [NC]
RewriteRule ^ /page1/%1? [R=302,L]
# Internally forward /page1/value to /page1.php?parameter-view=value
RewriteRule ^page1/(.*)/(.*)/?$ /page1.php?parameter-view=$1&parameter-id=$2 [L,QSA,NC]
# Internally forward /page1/value to /page1.php?parameter-view=value
RewriteRule ^page1/(.*)?$ /page1.php?parameter-view=$1 [L,QSA,NC]
# Redirect /page2.php?parameter-view=value to /page2/value
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page2\.php\?parameter-view=([^\s]+) [NC]
RewriteRule ^ /page2/%1? [R=302,L]
# Internally forward /page2/value to /page2.php?parameter-view=value
RewriteRule ^page2/(.*)/(.*)/?$ /page2.php?parameter-view=$1&parameter-id=$2 [L,QSA,NC]
# Internally forward /page2/value to /page2.php?parameter-view=value
RewriteRule ^page2/(.*)?$ /page2.php?parameter-view=$1 [L,QSA,NC]
I have a set of rewrites for page1 and page2. Is there a way to combine these rewrites so everytime I make a new page I can just add the page "page3" instead of adding a 3rd group of rewrites? Example of what I'm going for:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(page1|page2|page3)\.php\?parameter-view=([^\s]+) [NC]
RewriteRule ^ /(page1|page2|page3)/%1? [R=302,L]
RewriteRule ^(page1|page2|page3)/(.*)/(.*)/?$ /(page1|page2|page3).php?parameter-view=$1&parameter-id=$2 [L,QSA,NC]
RewriteRule ^(page1|page2|page3)/(.*)?$ /(page1|page2|page3).php?parameter-view=$1 [L,QSA,NC]
The best case scenario is i refine not only the idea of using the same rewrite for all pages but maybe not even having to use 3 to do the job?
Yes sure you can do something like this:
RewriteCond %{THE_REQUEST} \s/+(page1|page2|page3)\.php\?parameter-view=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
RewriteRule ^(page1|page2|page3)/(.*)/(.*)/?$ /$1.php?parameter-view=$2&parameter-id=$3 [L,QSA,NC]
RewriteRule ^(page1|page2|page3)/(.*)/?$ /$1.php?parameter-view=$2 [L,QSA,NC]

.htaccess, cannot get first part of a URL in rewrite condition

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

Resources