.htaccess URL structure with variables stops working w/ pagination - .htaccess

I have a problem with htaccess on one of these 2 pages: index.php & search.php
Both pages are functional & dependent on variables being sent to them via "GET" method.
SEARCH Example -
search.php?tag=KEYPHRASE
Showing as: site.com/tag/KEYPHRASE
# HTACCESS:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^tag/(.*)/?$ /search.php?tag=$1 [NC,L]
# internal forward
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^tag/(.+?)/?$ /search.php?tag=$1 [L,QSA,NC]
# external rewrite
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search\.php\?tag=([^\s]+) [NC]
RewriteRule ^ /tag/%1? [R=302,L]
INDEX Example -
index.php?id=ID&title=TITLE
Showing as: site.com/cover/TITLE-ID
# HTACCESS:
RewriteRule ^cover/(.*)-(.*)/?$ index.php?id=$2&title=$1 [NC,L]
# internal forward FOR SINGLE PAGE (index)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cover/(.+?)-(.+?)/?$ /index.php?id=$2&title=$1 [L,QSA,NC]
# external rewrite
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s]+)&title=([^\s]+) [NC]
RewriteRule ^ /cover/%2-%1? [R=302,L]
Both pages also have pagination, my problem is, it only works for search.php
SEARCH page w/ pagination:
site.com/tag/KEYPHRASE&page=# WORKS! Correct paginated results being returned.
INDEX page w/ pagination:
site.com/title&page=#-ID Inserts &page=# in between the title & ID in the URL, but shows results.
The only difference I see between these 2 are the number of variables being passed on, otherwise they are being rewritten exactly the same.
I need help with making my .htaccess structure work seamlessly w/ pagination query strings for my index.php page URL.

You need to use QSA flag in your internal rewrite rules and rearrange your rules.
Keep your .htaccess like this:
RewriteEngine On
# external rewrite
RewriteCond %{THE_REQUEST} \s/+index\.php\?id=([^&\s]+)&title=([^&\s]+)&page=([^&\s]+) [NC]
RewriteRule ^ /cover/%2-%1/%3? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+index\.php\?id=([^&\s]+)&title=([^&\s]+)\s [NC]
RewriteRule ^ /cover/%2-%1? [R=302,L]
# external rewrite
RewriteCond %{THE_REQUEST} \s/+search\.php\?tag=([^&\s]+)&page=([^&\s]+) [NC]
RewriteRule ^ /tag/%1/%2? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+search\.php\?tag=([^&\s]+)\s [NC]
RewriteRule ^ /tag/%1? [R=302,L]
# internal forward FOR SINGLE PAGE (index)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cover/([^-]+)-([^/]+)/([0-9]+)/?$ /index.php?id=$2&title=$1&page=$3 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cover/([^-]+)-([^/]+)/?$ /index.php?id=$2&title=$1 [L,QSA,NC]
# internal forward
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^tag/([^/]+)/?$ /search.php?tag=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^tag/([^/]+)/([0-9]+)/?$ /search.php?tag=$1&page=$2 [L,QSA,NC]

Related

Writing clean URLS with 2 parameters that point to index.php

Here is my url:
website.com/index.php?folder=cars&type=ford
and would like it to be website.com/cars/ford and all pages point to index.php that I will grab the params from the url.
I can't seem to get it, here is my .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?folder=$1&app=$2 [L]
This is one example I got from a clean url generator but its not working. Any ideas?
You can insert this rule just below RewriteEngine On to redirect old URL to pretty URL:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?folder=([^\s&]+)&type=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?folder=$1&app=$2 [L,QSA]

redirecting to localhost dashboard instead of project root

I have a site in localhost - ourallnews. I want to redirect all keyword - index, index.php, index.html to site root - localhost/ourallnews/. I have applied following rule in .htaccess but it is redirected to - localhost/dashboard/. How to fix it ?
RewriteEngine on
RewriteBase /ourallnews/
RewriteRule ^(.*)index\.(php|html?)$ /$1 [R=301,NC,L]
RewriteCond %{THE_REQUEST} /category(?:\.php)?\?cat=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)$ category.php?cat=$1 [L,QSA]
If you're using a RewriteBase then just use a relative link while redirecting:
RewriteEngine on
RewriteBase /ourallnews/
RewriteRule ^(.*)index\.(?:php|html?)$ $1 [R=301,NC,NE,L]
RewriteCond %{THE_REQUEST} /category(?:\.php)?\?cat=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ category.php?cat=$1 [L,QSA]
Make sure to clear browser cache or use a new browser for your testing.

Htaccess - Exclude a word from existing rewrites

I have the following in my htaccess file:
# drop tags
#RewriteCond %{THE_REQUEST} (.*)designs/(.*)/?tag=shirts [NC]
#RewriteRule .* /designs/%2/ [R=301,L]
RewriteCond %{THE_REQUEST} (.*)designs/([^?]+)\?tag=[^&]* [NC]
RewriteRule .* /designs/%2? [R=301,L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/designs/ [NC]
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]
RewriteCond %{QUERY_STRING} ^search= [NC]
RewriteRule ^designs/.*$ /$0? [L,R=301,NC]
RewriteCond %{QUERY_STRING} ^mfp= [NC]
RewriteRule ^designs/ %{REQUEST_URI}? [L,NC,R=301,NE]
My theme supports a blog but after enabling it (it defaults to example.com/blog/), when I click on the blog link in my menu, it takes me to a page that says "There is no product that matches the search criteria". When I remove the htaccess rules listed above, the blog page (which contains the article listings) works fine so it is definitely that.
How can I exclude the word "blog" from the htaccess rules so this issue disappears?
I managed to fix it after a couple hours of tinkering. The solution was:
# external redirect from actual URL to pretty one
RewriteCond %{REQUEST_URI} !^/blog($|/)$
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_URI} !^/blog($|/)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/designs/ [NC]
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]
This solution also handles /blog (without the trailing /).
Hope that helps someone in the future.

Alter URL with htaccess

I have a .htaccess file inside folder public (root/public)
I would like to achieve the following transformations:
/index.php -> /
index.php?site=siteName -> /siteName/
/siteName/ or /siteName -> serves as ?site=siteName
Here is my complete .htaccess file so far (with case 3 solved):
Allow from all
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ index.php?current=$1
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?current=$1
You can use an additional rules for redirection of old URL to pretty URL and index.php removal:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?current=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?current=$1 [L,QSA]

redirection and mapping for same page using htaccess

redirection and mapping for same page using htaccess not working
Ex . www.xyz.com?view=aa&param=bb -- redirect it to www.xyz.com/aa/bb
Then map www.xyz.com/aa/bb to
www.xyz.com?view=aa&param=bb
below is the rules.
redirection
RewriteCond %{QUERY_STRING} ^view=([^&]*)&p=([0-9]+)&/(.*)$
RewriteRule ^insights/(.+?)\.html$ insights/$1/%1/%2-%3.html? [R=301,L]
mapping
RewriteRule ^insights/(.*)/(.*)/([0-9]+)-(.*)\.html$ /insights/$1.html?view=$2&p=$3&/$4 [L,QSA,NC]
This should work to what you're trying to do:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirect /insights/anything.html?view=anything&p=anything&/anything
# To /insights/anything/anything-anything.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(insights)/([^.]+)\.html\?view=([^&\s]+)&p=([^&\s]+)&/([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3/%4-%5.html? [R=302,L]
# Internally forward /insights/anything/anything-anything.html
# To /insights/anything.html?view=anything&p=anything&/anything
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^-]+)-([^.]+).html$ /$1/$2.html?view=$3&p=$4&/$5 [L]
Change R=302 to R=301 only after you have confirmed the rule is working to avoid your browser from caching wrong redirects.
Keep these 2 rules at top of your .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\?view=([^&]+)&param=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /?view=$1&param=$2 [L,QSA]

Resources