301 redirect & htaccess RewriteEngine conflicting - php

My site uses RewriteEngine to hide variables in the URI as follows:
RewriteRule ^([^/\.]+)/?/([^/\.]+)/?$ page.php?theme=$1&pg=$2 [L]
This works fine, but as we're relaunching, we've got a few 301 redirects to handle as well. I've added these below my rewrite rules, here's an example:
redirect 301 /about/about-the-site https://www.mywebsite.com/about-us
The problem is, that it's redirecting but adding extra crap to the url string:
https://www.mywebsite.com/?theme=about&pg=about-the-site
Any idea why this would be happening?

Don't mix mod_alias and mod_rewrite rules as these 2 modules execute at different stages. Keep everything in mod_rewrite itself:
RewriteEngine On
RewriteRule ^about/about-the-site/?$ https://www.mywebsite.com/about-us? [L,NC,R=301]
RewriteRule ^([^/.]+)/?/([^/.]+)/?$ page.php?theme=$1&pg=$2 [L,QSA]
Also note extra ? in the target URL to strip any pre-existing query string. Make sure clear browser cache while testing it.

Related

Correct url rewriting rule for .htaccess?

I have a webshop in asp language that will be renewed to a php webshop. There are still many url's and links from the old webshop stored on different websites that I want to revert to the homepage of the new webshop.I'm looking for example for the correct rule to redirect the following url in my .htaccess file:
http://www.example.com/shopdisplayproducts.asp?id=15&cat=football to http://www.example.com
Could you show me this example, so I might be able to make the other redirects myself.
I have tried for example this, but without success:
RewriteRule ^shopdisplayproducts* index.php [R=301,L]
Thanks for your input
Regards,
Vicef
If you want a catch-all URL that will match regardless of the query string you could use
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^shopdisplayproducts\.asp / [L,R=301]
If you want to match the query string as well you need to add two lines
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{QUERY_STRING} id=15
RewriteCond %{QUERY_STRING} cat=football
RewriteRule ^shopdisplayproducts\.asp / [L,R=301]
While testing it's better to use R=302 instead of R=301 because browsers cache 301 responses, so if you change the rule you won't see the effect because the browser is still executing the cached result. When you use 302 you don't have this problem. So start with 302 and once it works replace that with 301.
Also, please be aware that redirecting everything to the homepage of a new site is almost never a good idea in terms of SERP ranking. If there is a new URL for a certain page it's better to rewrite to that URL. If there isn't, you might want to consider just letting it 404 so search engines will just remove them from their results.

.htaccess 301 redirect conflicting with rewriterule

I am trying to redirect the URL but its conflicting with the rewrite-rule, I am using this redirection Rule
Redirect 301 /antique-vintage-rugs/170-antique-oriental-rugs-carpets https://example.com/antique-vintage-rugs/170-antique-rugs
it took me to this link
https://example.com/antique-vintage-rugs/170-antique-rugs?action=clear&template=170-antique-oriental-rugs-carpets instead of this
https://example.com/antique-vintage-rugs/170-antique-rugs
My Rewrite URL is this
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^antique-vintage-rugs/(.+) results.php?action=clear&template=$1 [NC]
Is there any way I can get the desired output redirect link?
You need to only be using mod_rewrite here instead of mixing it with mod_alias (where the Redirect directive belongs). When you mix the two, both end up processing the same request.
So instead of Redirect, use:
RewriteRule ^antique-vintage-rugs/170-antique-oriental-rugs-carpets https://example.com/antique-vintage-rugs/170-antique-rugs [L,R=301]
but make sure it's before your other rules.
I had a somewhat similar problem. I was trying to add redirects from cpanel while I already had some rewrite rules written in my .htaccess file. The error I got was "No maching tag for "
What I ultimately did was that kept a copy of my existing rules and cleaned the .htaccess. Then went and added all the redirects that I needed from cpanel, and then at the end put back my own rewrite rules in the end of the file. That worked for me

.htaccess RewriteEngine converting links without changing links

I'm on my begining of learning PHP and very first steps of .htaccess, most of my new web is about main category and few subcategories.
Here are links examples i had before working out .htaccess RewriteEngine:
example.com/index.php?cat=email
example.com/index.php?cat=about&show=some
with help of .htaccess RewriteEngine i've convered them to:
example.com/email/
example.com/about/some/
Here is part of .htaccess:
RewriteRule ^([A-Za-z0-9-]+)/$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?cat=$1&show=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?cat=$1&show=$2 [L]
Now problem is that most of content have inside links like: "example.com/index.php?cat=about&show=some" Changing them all is option, but anyway... is there anything else could be done? I heard of some .htaccess option that autoconverts links to format you need without changing them manualy, so all links in PHP pages will be the same, but once user gets page loaded, links will be like (example.com/about/some/) Is there anything like that, or is there any other option to leave original link without changing them all?
Cheers!
Links on your site are created with your PHP scripts, Apache with htaccess can't magically change all this links in a raw.
Now what you can do, is redirect old URL to the new ones using htaccess, with a 301 redirection.
For example, if someone click on example.com/index.php?cat=email, Apache will redirect (= the URL will be changed and there will be another HTTP request) to example.com/email/, and then rewrite this to index.php?cat=email
Add this to your htaccess :
RewriteCond %{REQUEST_FILENAME} index.php$
RewriteCond %{QUERY_STRING} cat=([a-zA-Z0-9-]+)
RewriteRule ^ http://example.com/%1/? [L,R=301]
Anyway I strongly recommend you to change the links directly in your code, because even if the solution I've just explained should works (not tested), it's not really healthy to use redirection when you can avoid them.

htaccess redirect from asp to php with parameters

I have to redirect all my asp page to new website developed in PHP.
I had my asp page as,
abc.asp?id=82
Which need to be redirected to
siteurl/index.php/abc
Can any one help me with this in HTAccess?
I have tried with,
Redirect 301 /abc.asp?id=82 siteurl/index.php/abc
rewriterule ^/abc.asp?id=82$ siteurl/index.php/abc[R=301,L]
But this is not working and giving me a 404 error.
You can't match against the query string in either a Redirect or RewriteRule. It's not very clear what the scope you're trying to accomplish here. Is the id=82 important? Does abc mean "anything that's just letters"? Is siteurl a directory or a domain name? If it's strictly what you've attempted, then this is strictly how it'll work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=82($|&)
RewriteRule ^/?abc.asp$ siteurl/index.php/abc? [L,R=301]
You're making 2 main mistakes:
Rewrite rule matches only URI part and doesn't match query string
Rewrite rule in .htaccess doesn't match leading slash.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^id=82(&|$) [NC]
RewriteRule ^(abc)\.asp$ http://domain.com/index.php/$1 [R=302,L,NC]
Once you verify it to be working replace 302 (Temporary Redirect) with 301 (Permanent Redirect)
Just use
RewriteRule ^abc.asp$ siteurl/index.php/abc [R=301,QSA,L]
That should do the job.. Using the QSA flag will forward any GET paramater too, if that's what you meant with the title.

Forcing urls to rewrite/redirect?

This rewrite methode works, but its not Forcing urls to rewrite/redirect to the new urls.
I use this:
RewriteEngine on
RewriteRule ^page/([^/\.]+)$ search.php?q=$1
i can access rewrited urls (project/page/etc..) , but old urls/links (search.php?q=etc) still accessible without redirect.
note: i use $_SERVER variants to creating urls, and on localhost.
You have to rewrite project/page to search.php? in order to hide the ugly urls.
And redirect search.php? to project/page in order to make the canonical urls the only way to access that resource.
In your code there is no mention of the redirect, you're just rewriting.
Think of it this way
rewrite hides the ugly urls behind canonical urls (it hides them, it doesn't eliminate them);
redirect responds to the browser with a message like "search.php? has moved to project/page, try that link instead" and the browser follows the new link;
To redirect so called ugly URLs to SEO friendly URL you would need another Rewrite rule. Have your .htaccess code like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^page/([^/\.]+) search.php?q=$1 [L,QSA,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search\.php\?q=([^\s]+) [NC]
RewriteRule ^ page/%1? [R=302,L]
Once you have verified that it's working fine change R=302 to R=301.
You'd need a [R=301] on the rewrite rule to turn it into a client-side redirect. Otherwise it's purely an internal rewrite and the client will never see the url getting changed.
e.g.
RewriteRule ^page/([^/\.]+)$ search.php?q=$1 [R=301,L]

Categories