I have created two pages category.html and store-detail.html . I created each page in separated .htaccess file page is working fine but how to merge two .htaccess file in single file.
category.html (.htaccess)
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/category\.html\?id=([0-9]+)\s [NC]
RewriteRule ^ /category/%1? [R=301,L]
RewriteRule ^category/([0-9]+)$ /category.html?id=$1 [L]
store-detail.html (.htaccess)
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/store-detail\.html\?id=([0-9]+)\s [NC]
RewriteRule ^ /store-detail/%1? [R=301,L]
RewriteRule ^store-detail/([0-9]+)$ /store-detail.html?id=$1 [L]
Merged .htaccess file
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/category\.html\?id=([0-9]+)\s [NC]
RewriteCond %{THE_REQUEST} \s/store-detail\.html\?id=([0-9]+)\s [NC]
RewriteRule ^ /category/%1? [R=301,L]
RewriteRule ^ /store-detail/%1? [R=301,L]
RewriteRule ^category/([0-9]+)$ /category.html?id=$1 [L]
RewriteRule ^store-detail/([0-9]+)$ /store-detail.html?id=$1 [L]
I have created two dynamic pages category.html and store-detail.html, I am getting current URL this http://localhost/category/?id=10 and http://localhost/store-detail/?id=10 working fine, But I am changing URL structure of http://localhost/category/10 and http://localhost/store-detail/200. If I run category.html hide store-detail page line in htaccess and If I run store-detail.html hide category access line. How to Merge?
See below file:
Now currently working store-detail page
Options -MultiViews
RewriteEngine On
# redirect "/section.php?id=xxx" to "/section/xxx"
# RewriteCond %{THE_REQUEST} \s/category\.html\?id=([0-9]+)\s [NC]
RewriteCond %{THE_REQUEST} \s/store-detail\.html\?id=([0-9]+)\s [NC]
# RewriteRule ^ /category/%1? [R=301,L]
RewriteRule ^ /store-detail/%1? [R=301,L]
# internally rewrite "/section/xxx" to "/section.php?id=xxx"
# RewriteRule ^category/([0-9]+)$ /category.html?id=$1 [L]
RewriteRule ^store-detail/([0-9]+)$ /store-detail.html?id=$1 [L]
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 am trying to create multiple rewrite rules, so that a few pages will be redirected to certain pages, and the rest will be redirected to the start page. However, all my pages keep getting redirected to the start page.
This is the code I am using:
RewriteCond %{HTTP_HOST} ^site\.com/category\.php?s=1$ [NC]
RewriteRule (.*) http://site.co.uk/category/? [R=301,L]
RewriteCond %{HTTP_HOST} ^site\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.site\.com$ [NC]
RewriteRule (.*) http://site.co.uk/? [R=301,L]
Edit:
This is the full .htaccess:
Order deny,allow
DirectoryIndex default.php index.php
SetEnv DEFAULT_PHP_VERSION 5
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /flavours\.php?\s=1 [NC]
RewriteRule ^ http://site.co.uk/flavours/? [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?site\.com$ [NC]
RewriteRule ^ http://site.co.uk/? [R=301,L]
This is the link I am trying to access: www.site.com/flavours.php?s=1
HTTP_HOST cannot match REQUES_URI.
You can use:
# specific redirects
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /flavours\.php\?s=1 [NC]
RewriteRule ^ http://site.co.uk/flavours/? [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?flaverco\.com$ [NC]
RewriteRule ^ http://site.co.uk/? [R=301,L]
Make sure to clear your browser cache before testing this.
I am using following htaccess code in my project
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /apnaujjain
#redirect localhost/apnaujjain/page.php?page_id=1&album_id=1&action=contacts to localhost/apnaujjain/1/1/contacts.html
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&album_id=([^\s&]+)&action=([^\s&]+) [NC]
RewriteRule . %1/%2/%3.html? [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/([^./]+)\.html$ page.php?page_id=$1&album_id=$2&action=$3 [NC,L,QSA]
#redirect localhost/apnaujjain/page.php?page_id=1&action=contacts to localhost/apnaujjain/1/contacts.html
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&action=([^\s&]+)\s [NC]
RewriteRule . %1/%2.html? [R=301,L]
RewriteRule ^([^/]+)/([^./]+)\.html$ page.php?page_id=$1&action=$2 [NC,L,QSA]
#redirect localhost/apnaujjain/contacts.php to localhost/apnaujjain/contacts.html
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
RewriteRule !^admin/ /%1.html [NC,R=302,L,NE]
RewriteRule ^(.+?)\.html$ $1.php [L,NC]
#RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
#RewriteRule ^ /%1.html [R=302,L,NE]
#RewriteRule ^(.+?)\.html$ $1.php [L,NC]
Now everything is working fine except one thing I have some webservice also that I am calling from webservice folder, url is
http://localhost/apnaujjain/webservice/gethomepagecontent1.php
Now the problem is due to htaccess my webservice stopped working, it redirecting to wrong page so its not working. I don't want to apply htaccess rule on webservice folder. Please help, thanks in advance.
Just below RewriteBase /apnaujjain line add this line:
RewriteEngine On
RewriteBase /apnaujjain/
# add everything for webservice/...
RewriteRule ^webservice(/.*)?$ - [L,NC]
# rest of your existing rules
Currently my localhost URL is like localhost/apnaujjain/page.php?page_id=1&action=contacts
Now I want to write a rule so that when I go to above url it just rewrite the url like localhost/apnaujjain/contacts.html
See it should convert .php into .html without affect page content.
Until now I've tried
Options +FollowSymLinks -MultiViews
RewriteEngine on
#RewriteBase /
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.php?page_id=1$&action=2$
RewriteRule ^(.*)\.php $2.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html $1.php [L]
But it's not working.
You can use this code in /apnaujjain/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /apnaujjain/
# redirect localhost/apnaujjain/page.php?page_id=1&action=contacts to
# localhost/apnaujjain/1/contacts.html
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&action=([^\s&]+) [NC]
RewriteRule . %1/%2.html? [R=301,L]
RewriteRule ^([^/]+)/([^.]+)\.html$ page.php?page_id=$1&action=$2 [NC,L,QSA]
I have created a new Wordpress site that will replace a multi-domain website.
Server redirections has been done on different domains to point all of them to the same path.
I need do redirect pages based on the parent domain and also, redirect specific domain pages to new specific domain pages .
For example, when "www.mysite-example.com.au/contact" is requested it should redirect to "mysite.com.au/contact-us" and also "www.mysite-example.com.au/contact/form" should redirect to "mysite-example.com.au/request-contact-form"
Here is my current htaccess where I'm doing global redirection
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.abilitypeople.com$ [NC]
RewriteRule ^(.*)$ http://abilitypeople.co.uk/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www.abilitypeople.co.uk$ [NC]
RewriteRule ^(.*)$ http://abilitypeople.co.uk/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www.abilitypeople.com.au$ [NC]
RewriteRule ^(.*)$ http://abilitypeople.com.au/ [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Does any one know how to create these redirections on the same htacces
"www.mysite-example.com.au/contact" -> "mysite.com.au/contact-us"
"www.mysite-example.com.au/contact/form" -> "mysite-example.com.au/request-contact-form"
"www.mysite-example.co.uk/contact" -> "mysite.co.uk/contact-us"
"www.mysite-example.co.uk/contact/form" -> "mysite-example.co.uk/request-contact-form"
By the way, thanks to anubhava for his help
You can have your rules like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?abilitypeople\.com$ [NC]
RewriteRule ^ http://abilitypeople.co.uk%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^www\.(abilitypeople\.(?:co\.uk|com\.au))$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^contact/?$ contact.php [L,QSA]