What is wrong with RewriteRule? - apache

i have following url structure
http://www.mydomain.com/vpn-providers.php?st=30&page=2
and .httaccess rules for making seo are
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
RewriteRule ^vpn-providers([^-]*)-([^-]*)\.html$ /vpn-providers.php?st=$1&page=$2 [L]
the above code is used in pagination urls.
i want to make URLs a bit SEO friendly...My required out put is
http://www.mydomain.com/vpn-providers-2.html (2 is the page number
here ie page=2)
Can someobdy tell me where i am doing error? How can i modify my htaccess rules?

Related

Mod_rewrite redirect not working as intended (working in htaccess checker)

I'm trying to redirect users accessing https://www.mywebsite.com/index.php?p=home to https://www.mywebsite.com/. I have already added the code below to my public_html's .htaccess file and have tested it on an htaccess checker.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteCond %{QUERY_STRING} ^p=home$ [NC]
RewriteRule .* https://%{HTTP_HOST}/ [R=301,L,QSD]
Based on my code, do you have any idea why the redirect is not working even though the htaccess checker says it should be working.
You could try following Rules in your htaccess rules file. Please don't trust htaccess checker sites they don't give correct results. Have your htaccess file with following rules and make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
##Redirection external rules here.
RewriteCond %{THE_REQUEST} \s/index\.php\?p=home\s [NC]
RewriteRule ^ https://%{HTTP_HOST} [R=301,L]
##Rewrite internal rule here.
RewriteRule ^/?$ index.php?p=home [L]

how to Rewrite two parameters URL to SEO friendly URL and Redirect to SEO friendly url

This is my page url example.com/platform/bidProject.php?pID=JCVGK&name=Proof%20Reading%20Blogs
These rules helped me to Rewrite this as example.com/platform/project-bids/JCVGK/Proof-Reading-Blogs/
RewriteRule ^platform/project-bids/(.*)/(.*)/?$ /platform/bidProject.php?pID=$1&name=$2 [L,NC]
RewriteCond %{THE_REQUEST} \s/platform/bidProject.php?pID=$1&name=$2
RewriteRule ^platform/bidProject.php?pID=$1&name=$2 /platform/project-bids/(.*)/(.*)/ [NC,R=301,L]
but the issue is if I visit to the page example.com/platform/bidProject.php?pID=JCVGK&name=Proof%20Reading%20Blogs the url stays same. I want this to be redirected to example.com/platform/project-bids/JCVGK/Proof-Reading-Blogs/
So I tired this:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^(\S+)$ /$1 [NE,R=302,L]
RewriteRule ^([^/]+)/([^/]+)/?$ /platform/bidProject.php?pID=$1&v=$2 [L,QSA]
Seems I am doing something wrong with this because it makes no any affect on this.
I found a solution to redirect using JavaScript but I like to have .htaccess solution because JavaScript can be disable and can be seen in the source code. The basic intention for doing this fails here.
How can I achieve this using .htaccess
My htaccess path is example.com/.htacsess
You can use these Rules
RewriteEngine on
#redirects /platform/bidProject.php. php?pid=val1&name=val2 to /platform/project-bids/val1/val2/
#redirects the old url to the new one
RewriteCond %{THE_REQUEST} \s/platform/bidProject.php\?pID=([^&]+)&name=([^&\s]+) [NC]
RewriteRule ^ /platform/project-bids/%1/%2/? [NC,R,L]
# rewrites or internally maps the new url to the old one
RewriteRule ^platform/project-bids/(.*)/(.*)/?$ /platform/bidProject.php?pID=$1&name=$2 [L,NC]
Or
RewriteEngine on
RewriteCond %{ENV_REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^pid=([^&]+)&name=([^&]+)$ [NC]
RewriteRule ^platform/bidProject.php$ /platform/project-bids/%1/%2/? [NC,R,L]
RewriteRule ^platform/project-bids/(.*)/(.*)/?$ /platform/bidProject.php?pID=$1&name=$2 [L,NC]
Change R to R=301 (permanent redirect) when you are sure the rule is working ok.

Redirecting a domain with htaccess with a few specific 301s

Is it possible to do something like this to redirect old traffic to the new site with a 301.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !new-example.com$ [NC]
RewriteRule ^(.*)$ http://new-example.com/$1 [L,R=301]
However, I will need to manually map some pages to their new equivalent pages like
Redirect 301 /about http://new-example.com/about-us
Is it possible to do both?
If you are going to use Rewrite, then use it only. I don't recommend use both mod-alias and mod-rewrite. You can use rewrite to map to individual pages too. Also the order matters. The catchall rule should be the last one. This would provide cleaner code IMO.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^new-example.com$ [NC]
RewriteRule ^about/?$ http://new-example.com/about-us [R=301,L]
RewriteCond %{HTTP_HOST} !new-example.com$ [NC]
RewriteRule ^(.*)$ http://new-example.com/$1 [L,R=301]

How to use .htaccess to redirect non-www pages to www pages with the exception of a single sub-folder

I'm trying to redirect all non www. urls leading to my site to www. urls - however, there's one subfolder on my site I don't want changed. The code I've been working with is:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
Is there a way to tell this piece of code to ignore domain.com/specialsubfolder?
Thanks a lot, tried searching for this, but couldn't quite find what I'm looking for.
The common way (easy to read and understand) is to add one more condition to ignore this rule if URL starts with /specialsubfolder :
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/specialsubfolder
RewriteRule .* http://www.domain.com%{REQUEST_URI} [R=301,L]
Alternatively, add such condition into matching pattern (more difficult to read but a tiny bit faster):
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(?!specialsubfolder).* http://www.domain.com%{REQUEST_URI} [R=301,L]
P.S. You can add [NC] flags if case-insensitivity is required.

Using .htaccess to redirect domain.co.uk/index.html to www.domain.co.uk

I have noticed that search engines have been crawling both the domain.co.uk and www.domain.co.uk versions of a web site that I've recently developed.
Using .htaccess I have been able to setup http 301 redirects so that:
http://domain.co.uk is redirected to http://www.domain.co.uk
and
http://www.domain.co.uk/index.html is redirected to http://www.domain.co.uk
However:
http://domain.co.uk/index.html does not get redirected to http://www.domain.co.uk as I would expect. Instead the redirect goes to: http://www.domain.co.uk/http://www.domain.co.uk/
The contents of my .htaccess are (obviously I have replaced my domain to simplify matters):
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^domain.co.uk [nc]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,nc]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.domain.co.uk/$1 [R=301,L]
I know I am probably missing something obvious but please could someone help me get the final redirect to http://www.domain.co.uk working as expected?
The first part of your rule set is working fine, the missing www. is added correctly. For the second part you only need a simple rule to remove the index.html without using any additional condition:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,NC]
RewriteRule ^index.html$ http://www.domain.co.uk/ [R=301,L]
I think this should do the trick.