How to rewrite /?a=a to /a/? - apache

How can I rewrite this URL:
http://mywebsite.com/?a=a
To this URL:
http://mywebsite.com/a/
With htaccess?
I tried this rewrite rule but it's not working:
RewriteRule /a /?a=a

Try this instead :
RewriteRule ^a/?$ /?a=a [L]
This will route http://mywebsite.com/a/ requests to http://mywebsite.com/?a=a

If you are only looking for an exact match of ?a=a in the URI you can use this.
RewriteRule ^\?a=a$ /a/ [R=301]
This assumes you want to rewrite the actual URL in the user's browser and provide a 301 header to let anything crawling the page know about the "proper" URI to use (thus the [R=301]).

Related

Htaccess remove string from URL behind last slash

Looking for solution how to remove anything behind URL last slash.
example output URL:
http://example.com/pictures/123/category-name/
problems to avoid for this URL:
http://example.com/pictures/123/category-name/blah.html
http://example.com/pictures/123/category-name/blah/blah.html
http://example.com/pictures/123/category-name/blah/blah/blah.html
http://example.com/pictures/123/category-name/blah/
http://example.com/pictures/123/category-name/blah
So far i have this:
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/category-name/$ $1/$2/category-name/ [R]
RewriteRule ([A-Za-z0-9-]+)/([0-9]+)/category-name/(.*)$ category.php?VarA=$1&VarB=$2
it works for output URL and the URL with anything behind pattern. Problem is that it doesn't rewrite (redirect) to output URL if anything is behind last slash.
I'd personally use logic which is straight-forward like this:
#redirect to canonical url if no ending slash
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/category-name$ $1/$2/category-name/ [R,L]
#redirect to canonical url if anything behind slash
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/category-name/(.+)$ $1/$2/category-name/ [R,L]
#rewrite to PHP file if visiting the canonical url
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/category-name/$ category.php?VarA=$1&VarB=$2 [L]

htaccess - how to redirect url with params to base url

I am trying to redirect a page.html with params at the end to the base url without params. I know I am supposed to use mod rewrite but I can't get the rules figured out. Your help will be appreciated.
Here is the scenario:
I tried this in htaccess and this rule didn't work:
redirect 301 "/page.html?p=2" http://www.domain.com/page.html
I understand from reading other posts on stackoverflow that I should write something like
^page\.html$ http://www.domain.com/page.html [QSA,NC,R=301,L]
The rules at the end [QSA,NC,R=301,L] however are incorrect. I can't figure it out. I simply want to do a 301 for this url with the p=2 parameter, and no other param.
And then process the remaining rules after this rule for other pages.
You can put this code in your root htaccess
RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=2$ [NC]
RewriteRule ^page\.html$ /page.html? [R=301,L]

How to redirect URL Friendly created with rewrite

I created URL with rewrite mod.
This is the command:
RewriteRule ^productos/(.+)-(.+).html ecommerce/buscador.php?categoria=$2
Example the URL
http://www.frutasadomicilio.cl/productos/Verduras-2.html
The URL works good but now i need change the URL. I am thinking that with redirect 301 is my way an a solution. But when i try the redirect the URL, the command return the new url but with parameter.
The command is:
redirect 301 /productos/Verduras-2.html /productos/Verduras_a_domicilio-2.html
when I access the URL from google search, the Browser return this URL with parameter
http://www.frutasadomicilio.cl/productos/Verduras_a_domicilio-2.html?categoria=2
Any suggestion?
Thanks
Do not mix Redirect and RewriteRule for similar URLs. I suggest you to use only RewriteRule as
RewriteEngine on
RewriteRule ^productos/Verduras-2\.html$ /productos/Verduras_a_domicilio-2.html [R=301,L]
RewriteRule ^productos/(.+)-(.+)\.html$ ecommerce/buscador.php?categoria=$2 [NC,L]
Also, the order of the rules above is important.

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]

Mod rewrite / remove slash behind .html

I need a mod rewrite 301 that remove a slash behind the .html from the url.
Example:
Good URL: http://www.example.org/some-stuff-t123.html
Bad URL: http://www.example.org/some-stuff-t123.html/abcdesftesttest
The Bad URL with a / after the html should rewrited (301) to the good URL.
I need a request URI that detect a /* after the html and if yes, rewrite it.
Thank you
Try:
RedirectMatch 301 ^/(.*)\.html/ /$1.html
or using mod_rewrite:
RewriteRule ^(.*)\.html/ /$1.html [L,R=301]
If you have other rewrite rules, make sure the one that redirects is before any rules you may already have.