.htaccess redirection rule - .htaccess

Can anyone help with .htaccess URL rewriting rule.
I need to redirect pages like this:
www.website.com/index.php?a=some-word-some-other-word
www.website.com/index.php?a=something
www.website.com/index.php?a=some-other-thing
All that pages have the same content as www.website.com/index.php
to
www.website.com/index.php or www.website.com/
I already asked for this and I got this:
RewriteEngine On
RewriteBase /
#if the query string has an a parameter
RewriteCond %{QUERY_STRING} (^|&)a= [NC]
#Redirect and remove query string parameters
RewriteRule .* http://www.website.com/? [R=301,L]
It does that, it redirect those pages to home page (root of site). But I have some pages with URLs like:
i.php?a=something-something-something
and problem is that .htaccess code above affect those links too.
Can anyone make that code just to redirect links for pages based on index.php?a= and not for i.php?a= too.

Try this and tell me if it works:
RewriteEngine On
RewriteBase /
#if the query string has an a parameter
RewriteCond %{QUERY_STRING} (^|&)a= [NC]
#Redirect and remove query string parameters
RewriteRule ^index\.php$ http://www.website.com/? [R=301,L]

Related

How to redirect a URL wildcard using htaccess

My site was hacked not too long ago and a lot of URLs were created in the following format:
http://example.com/prematurely.asp?skin=pspfsffdproblems=nq....
is there anyway I can re-direct all these wildcard URLs based on prematurely.asp to a single page? for example:
http://example.com/newpage
.htaccess file is as follows:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^nowcosmetic\.co.uk$ [NC]
RewriteRule ^(.*)$ http://nowcosmetic.co.uk/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/questions-and-answers\.html$
RewriteRule .* http://nowcosmetic.co.uk/botox.html [R=301,L]
RewriteRule ^prematurely\.asp /botox? [R=301,L]'
You can do this by adding a rewrite rule
RewriteEngine on
RewriteRule ^prematurely/(.*) /newpage [R=301,L]
As mentioned in comments, if these URLs are the result of a hacked site which has now been resolved then you are better off (SEO wise) serving a 404 (or 410 Gone) instead of redirecting.
However, if you still want to redirect...
re-direct all these wildcard URLs based on prematurely.asp to a single page
At the top of your .htaccess file (in your document root):
RewriteEngine on
RewriteRule ^prematurely\.asp /newpage? [R=301,L]
Note that RewriteEngine On should only appear once in your file (at the top).
This simply matches against "prematurely.asp" (ignoring the query string - as per your request). The trailing ? on the RewriteRule substitution strips the original query string from the rewritten URL.

Redirect page on subdomain only when no URL query string is present

Pretty specific htaccess question: I have a page at http://subdomain.example.com/page/ that contains a form. The page should only be accessed with auto-created links that contain query strings that feed information through the form. What I need is an htaccess rule that will redirect traffic coming to this page to my homepage at http://example.com/ whenever it is reached without a query string in the URL.
Put another way: if someone reaches http://subdomain.example.com/page/ they need to redirect to http://example.com. But if someone reaches http://subdomain.example.com/page/?querystring they need to stay on the page.
How do i solve this with htaccess?
try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^page/ http://example.com/ [L,R]
or
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteCond %{THE_REQUEST} !\?
RewriteRule ^page/ http://example.com/ [L,R]

dynamic to static url AND removing the dynamic

I want to change alle dynamic url's to static ones,
but after rewriting the dynamic url's are still responding/available.
What did I do =>
I found this Tool for SEO:
http://www.webconfs.com/url-rewriting-tool.php
I entered this:
.../filmdetails.html?var=ich_einfach_unverbesserlich_ii
Then I put into my .htaccess this:
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^filmdetails/(.*)/$ filmdetails.html?var=$1
Works, but now I got a problem: This URL is still available and should not be: .../filmdetails.html?var=ich_einfach_unverbesserlich_ii
How do I get rid of the dynamic url's?
Your rule only rewrites the nicer looking URL to the one with a query string. Rules only work from a "pattern" -> "target" way, the mapping won't magically work the other way. You'll have to create a separate rule in order to redirect the browser:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /filmdetails\.html\?var=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /filmdetails/%2/?%3 [L,R=301]
Try:
Options +FollowSymLinks
RewriteEngine On
#set this to path to your filmdetails.html file (from the document root)
RewriteBase /
#checking if redirect already happened
RewriteCond %{ENV:REDIRECT_PASSED} !^$
RewriteRule $ - [L]
#Your rewrite rule
RewriteRule ^filmdetails/(.*)/$ filmdetails.html?var=$1 [L,E=PASSED:1]
#redirecting from filmdetails.html with query string ?var=something
RewriteCond %{QUERY_STRING} ^var=(.+)$ [NC]
RewriteRule ^filmdetails.html$ filmdetails/%1/? [R]
filmdetails.html?var=something will be redirected to filmdetails/something

.htaccess rewrite rule ,last query string is append to the url

Hi i make one cms sites and i need to rewrite my url
current my url is http://www.example.com/index.php?link=pages&cmsid=2&cmsLink=Carpet
it refers the cmsLink
I want my url like http://www.example.com/Carpet
I am using the following code
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^index.php?link=(.*)&cmsid=(.*)&cmsLink=(.*) $3
To achive this url it not directly possible with .htaccess
I use regular expression and other tings in htacess as well
I put remove the cmsid
current my url is http://www.example.com/index.php?link=pages&cmsLink=Carpet
RewriteCond %{REQUEST_URI} !/admin
RewriteCond %{REQUEST_URI} !^/(.*).php
RewriteBase /
RewriteRule ^([^/]+)/?$ index.php?link=pages&cmsLink=$1&%{QUERY_STRING} [L]
it return me http://www.example.com/Carpet
Try changing your last rule to this:
RewriteRule ^(.+)$ /index.php?link=pages&cmsid=2&cmsLink=$1
Since you want to have url like http://www.example.com/Carpet, so cmsid and link in your url has to be hardcoded to 2 and pages.

301 redirect from query string to simple URL

I've had a good look through the first ten pages of search results for "301 redirects" and can't find the answer so here goes...
I've moved from a crappy old CMS that didn't give my pages nice URLs to one that does and I want to set up a 301 redirect for my key pages.
Current URL: http://www.domain.com/?pid=22
New URL: http://www.domain.com/contact/
I'm using Wordpress and my current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any help would be awesome!
Give this a try. All you need to do is check to see if you are on page X and then redirect to page Y. Consider RewriteCond statements to be 'if' statements. If you need more redirects just duplicate the last two lines and edit the paths.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com\/?pid=22$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/contact [L,R=301]
You have to check the query string for the value in the "pid" variable and then redirect if the value in that variable matches a page you want to redirect. You can do this with the "RewriteCond" and "RewriteRule" directives like this:
RewriteEngine On
RewriteBase /
# Redirect pid=22 to http://www.domain.com/contact
RewriteCond %{QUERY_STRING} ^pid=22$
RewriteRule ^(.*)$ http://www.domain.com/contact [R=301,L]
You can repeat the "RewriteCond" and "RewriteRule" directives to create additional redirects.

Resources