Adding some extra rewrite code to existing main rewrite code - apache

I currently have this in my .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteRule ^(.*),(.*)$ $2.php?rewrite_params=$1&page_url=$2
RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]
I want to add some new code that rewrites:
stores/some-name/
to:
shop.php?shop_id=some-name
How can I do this without affecting the other rewrites or the other rewrites affecting the addition?

You can add your new rule in the end:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteRule ^(.*),(.*)$ $2.php?rewrite_params=$1&page_url=$2 [L,QSA]
RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]
RewriteRule ^stores/([0-9a-z.-]+)/?$ shop.php?shop_id=$1 [L,QSA,NC]

Related

How to write clean url in reverse?

I have the following in my my htaccess
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^terms-and-conditions terms.php
RewriteRule ^privacy privacy.php
RewriteRule ^thank-you thanks.php
How could I also do this in reverse such as typing /privacy.php is rewritten as /privacy.
You need to use 3 separate rules using %{THE_REQUEST} variable to avoid redirect loop:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{THE_REQUEST} \s/+terms\.php[\s?] [NC]
RewriteRule ^ /terms-and-conditions [R=301,L]
RewriteCond %{THE_REQUEST} \s/+privacy\.php[\s?] [NC]
RewriteRule ^ /privacy [R=301,L]
RewriteCond %{THE_REQUEST} \s/+thanks\.php[\s?] [NC]
RewriteRule ^ /thank-you [R=301,L]
RewriteRule ^terms-and-conditions/?$ terms.php [L,NC]
RewriteRule ^privacy/?$ privacy.php [L,NC]
RewriteRule ^thank-you/?$ thanks.php [L,NC]

Add HTTPS to already existing Mod Rewrite

I have the following rules and conditions in my mod_rewrite and like to add HTTPS to it. I like to confirm the following it accurate before publishing it live.
Current Rules and Conditions
RewriteEngine On
RewriteCond %{REQUEST_URI} \.jpg$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /home/domain.com/public_html/missing.png [L]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^index.html$ http://www.domain.com/ [L,R=301]
New Rules and Conditions
RewriteEngine On
RewriteCond %{REQUEST_URI} \.jpg$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /home/domain.com/public_html/missing.png [L]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTPS} Off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^index.html$ https://www.domain.com/ [L,R=301]
RewriteEngine On
# always do domain/https redirs first
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} ^(?!www\.domain\.com$). [NC]
RewriteCond %{THE_REQUEST} ^\S+\s+/?(\S*)
RewriteRule ^ https://www.domain.com/%1 [NS,NE,L,R=301]
# remove index files from URLs, except from POST forms, etc. you can add more file extensions to the second condition
RewriteCond %{REQUEST_METHOD} ^(?:GET|HEAD)$
RewriteCond %{THE_REQUEST} ^\S+\s+/?((?:[^?/]*/)*?)index\.(?:html?|php|pl)(\?\S*)?(?:\s|$)
RewriteRule ^ https://www.domain.com/%1%2 [NS,NE,L,R=301]
# missing images
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(?:jpe?g|png|gif)$ /missing.png [NS,NC,L,DPI]

Redirect Subdomain to a specific page

I am trying to have http://pk.domain.com to be redirected to http://www.domain.com/home.php?country=pk and similarly http://www.domain.com/pk/ to http://www.domain.com/home.php?country=pk
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/?$ adlisting.php?country=%1&category=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ adlisting.php?country=%1&location=$1&category=$2 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([a-z]{2})/([^/]+)/?$ adlisting.php?country=$1&category=$2 [L,QSA,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([a-z]{2})/([^/]+)/([^/]+)/?$ adlisting.php?country=$1&location=$2&category=$3 [L,QSA,NC]
This question is linked to Multi-Country Using .htacces
Add 2 rules for landing page before other rules:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$ [NC]
RewriteRule ^/?$ home.php?country=%1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([a-z]{2})/?$ home.php?country=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/?$ adlisting.php?country=%1&category=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ adlisting.php?country=%1&location=$1&category=$2 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([a-z]{2})/([^/]+)/?$ adlisting.php?country=$1&category=$2 [L,QSA,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([a-z]{2})/([^/]+)/([^/]+)/?$ adlisting.php?country=$1&location=$2&category=$3 [L,QSA,NC]

Add slashes and redirect pages with htacess and apache

Im trying to make it so everything with news.php in the url gets redirected to a url which strips out the news.php and adds a trailing slash. So http://www.example.com/news.php/news/article to http://www.example.com/news/article/
Ive been able to remove the news.php with this
RewriteRule ^(news|health|tips|weight|P[0-9]{2,8})$ $1/ [NC]
RewriteCond $1 ^((news|health|tips|weight|P[0-9]{2,8})/) [NC]
RewriteRule ^(.*)$ /news.php/$1 [L]
Ive tried adding a trailing slash to the above line and it does nothing.
I have also tried the stuff below, which adds a slash but doesnt remove the news.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /news.php/$1/ [R=301,L]
Can someone help me out here Im not sure what to do?
I also have some other rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^(ACT=.*)$ [NC,OR]
RewriteCond %{QUERY_STRING} ^(URL=.*)$ [NC,OR]
RewriteCond %{QUERY_STRING} ^(CSS=.*)$ [NC]
RewriteRule ^$ /news.php\?&%{QUERY_STRING} [L]
RewriteRule ^(.*)$ $1.php [L,QSA]
RewriteCond %{HTTP_HOST} !^www2\.
RewriteRule ^(.*)$ http://www2.%{HTTP_HOST}/$1 [R=301,L]
You need an additional rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} /news\.php/\S+ [NC]
RewriteRule ^news\.php/(.+?)/?$ /$1/? [R=301,L]
RewriteCond $1 ^((news|health|tips|weight|P[0-9]{2,8})/) [NC]
RewriteRule ^(.+)$ /news.php/$1 [L]
EDIT: To add a trailing slash to all non-files:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
Ok so I changed the first block anubhava suggested to
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
and now everything seems to be working as I want.

Rewrite rule doesn't change the browser url

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