External and internal redirection using htaccess - apache

I want to convert http://mywebsite.com/folder/file.html to http://mywebsite.com/file.
I am not using regular expression as first I am only concerned about this one URL only.
I tried this-
Redirect 301 /folder/file.html http://mywebsite.com/file
Using this I am able to externally redirect this URL to the desired one but since the URL doesn't exist so I am getting 404.
Now, in order to internally redirect the new URL to the old one, I am using below command but it doesn't seem to work-
RewriteRule http://mywebsite.com/file http://mywebsite.com/folder/file.html [L]

Use only mod_rewrite directives and use THE_REQUEST variable for external redirection.
# turn on rewrite engine
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+/folder/file\.html[\s?] [NC]
RewriteRule ^ /file [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^file/?$ /folder/file.html [L,NC]

Related

How to redirect the page after rewrite the URL

I have to rewrite the URL so I have used the below code in my htaccess which is working
RewriteRule ^products/part/pumps/?$ pumps.php [L,NC]
if someone tries to access the url link exmple.com/products/part/pumps/ then it's showing the pumps.php data.
Now my issue is if some try to access the page example.com/pumps.php then how can I redirect to this link exmple.com/products/part/pumps/
I have tried but getting a redirecting error
Redirect 301 /pumps.php /products/part/pumps/
I have many pages and i have to redirect them also. sharing two example here
RewriteRule ^power\.php$ /products/engine/power/ [R=301,L]
RewriteRule ^connecting\.php$ /products/connection/power/connecting/ [R=301,L]
Use the following before your existing rewrite:
# External redirect
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^pumps\.php$ /products/part/pumps/ [R=301,L]
(But test first with a 302 redirect to avoid potential caching issues.)
By checking that the REDIRECT_STATUS environment variable is "empty" we can redirect only direct requests from the user and not the rewritten request by the later rewrite. After the request is successfully rewritten, REDIRECT_STATUS has the value 200 (as in 200 OK HTTP status).
The RewriteCond (condtion) directive must precede every RewriteRule directive that triggers an external redirect.
The Redirect directive (part of mod_alias, not mod_rewrite) is processed unconditionally and will end up redirecting the rewritten request, resulting in a redirect loop. You need to use mod_rewrite throughout.
Use the END flag instead of RewriteCond (requires Apache 2.4)
Alternatively, you can modify your existing rewrite to use the END flag (instead of L) to prevent a loop by the rewrite engine. The RewriteCond directive as mentioned above can then be omitted. But note that the END flag is only available on Apache 2.4+.
For example:
# External redirects
RewriteRule ^pumps\.php$ /products/part/pumps/ [R=301,L]
# Internal rewrites
RewriteRule ^products/part/pumps/?$ pumps.php [END,NC]
It is advisable to group all the external redirects together, before the internal rewrites.
Unfortunately, due to the varying nature of these rewrites it doesn't look like the rules can be reduced further.

Opencart 301 Redirects

Having a problem with redirect in a .htaccess file on an Opencart store.
It seems that any URL with /index.php?_route_= isn't getting redirected.
For example, this works:
redirect /old-url-here http://example.com/new-url?
This doesn't:
redirect /index.php?_route_=some-url.asp http://example.com
Any idea or suggestions as to what might get this to work?
You can't match against the query string using mod_alias' Redirect directive. You'll need to use mod_rewrite, and if you use mod_rewrite, you're probably going to want to stop using mod_alias altogether.
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} route=some-url\.asp
RewriteRule ^index\.php$ http://example.com/
Another thing is - apart from Jon's answer - that URLs like index.php?_route_=some-keyword are used/created only internally and only in case you have the SEO turned on. In this case, you click on a link with URL like http://yourstore.com/some-keyword and this URL is rewritten into index.php?_route_=some-keyword.
Therefore you shouldn't be creating URLs like that on your own nor try to redirect from them. If you need to redirect, catch the first SEO URL to redirect it.
We do not know where did you put your changes into the .htaccess file, but if you take a close look at this rewrite rule
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
not only you'll find out it is the last rule there, but it also has this L switch which is telling Apache server that if this rule is matched, do a URL rewrite(, attach the query string [QSA] and) stop matching other rules [L] - this is the Last one.
If you need to redirect only a specific URL, do it the same way as redirect for sitemap.xml is done (for example) and place it before the last rewrite rule:
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
# ...
# your new rule here - while the URL in the link is http://yourstore.com/some-url.asp
RewriteRule ^some-url.aspx$ index.php?route=some-folder/some-controller [L]
# ...
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

Using a .htaccess to RewriteRule and Redirect 301 at the same time?

I have a couple of specific URLs that I want to display differently on my website. For example I want "/contact.php" to become "/contact". So I added this to my .htaccess:
RewriteRule ^contact$ contact.php
And to avoid having 2 different URLS pointing to the same page, I also want to do a 301 redirect between the old URL and the new one:
Redirect 301 /contact.php http://www.example.com/contact
Each of the line above works well separately. But if I add them both in my htaccess, I have a redirect loop. How can I fix that?
In the end, if I either type "/contact" or "/contact.php", I want to see the contact page with the url "/contact".
Edit: I also tried things like that, and it doesn't work:
RewriteRule ^/contact\.php$ http://www.example.com/contact [R=301,L]
RewriteRule ^/contact$ /contact.php [L]
Yes it will indeed cause redirection loop since mod_rewrite rules are applied in a loop. Here value of REQUEST_URI changes to contact.php after first rule and to contact by your second rule.
To avoid this looping you need to use %{THE_REQUEST} in your external redirect rule as THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some other rewrite rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1.
Use this:
RewriteEngine On
# external redirect from /contact.php to /contact
RewriteCond %{THE_REQUEST} \s/+(contact)\.php\[\s?] [NC]
RewriteRule ^ /%1? [R=302,L]
# internal forward from /contact to /contact.php
RewriteRule ^(contact)/?$ $1.php [L,NC]
Change 302 to 301 once you make sure it is working fine for you.

url rewriting not working automatically

I have problem with url rewriting.
I want to change dynamic link example.com/new.php?name=latest-news to example.com/latest-news-of-the-day my .htaccess file configuration is
RewriteEngine on
RewriteRule (.*)-of-the-day$ new.php?name=$1[NC,L]
Now the page url is still the same, but if I write manually example.com/latest-news-of-the-day it works. I want to display new url according to .htaccess file but it is not showing automatically.
You need to add another rule to externally redirect the browser when they request the new.php page:
RewriteCond %{THE_REQUEST} \ /+new\.php\?name=([^&\ ]+)
RewriteRule ^ /%1-of-the-day? [L,R=301]
Your incoming URI is /<something>-of-the-day, and you want to "invisibly" change it to the real file /new.php?name=<something>?
RewriteEngine On
RewriteRule ^(.*)-of-the-day/?$ /new.php?name=$1 [NC,QSA]
The [L] flag is unnecessary if there is no RewriteCond, and the [QSA] flag preserves any Query String that came in with the original URI. Note that it does not check if the original URI was an existing directory or file (you can add RewriteCond statements to restrict that).

Apache htaccess 301 redirect

I want to rewrite url from article.php?id=123 to article/123
I added the following rule, it works fine
RewriteRule ^article/(.*) /article.php?id=$1 [PT]
I also want to add a 301 redirect rule to let search engine know article.php?id=123 should move to article/123. I added the following rule but seems not working.
RewriteRule ^article.php?id=(.*)$ /article/$1 [R=301,L]
You need to match against the actual request and not the URI because the URI gets rewritten by other rules:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /article\.php\?id=([^\ ]+)
RewriteRule ^article\.php$ /article/%1? [R=301,L]
If you don't match against the actual request, a redirect loop will occur:
Browser requests /article.php?id=123
Server redirects to /article/123
Browser requests /article/123
Server rewrites internally to /article.php?id=123
Server internally matches and redirects the browser to /article/123
Browser requests /article/123
repeat from 4.
Try like this:
RewriteRule ^article.php?id=(.*)$ /article/$1 [L,R=301]