Redirecting complex URL Aliases in htaccess - apache

I need to redirect sample.com/newdomain/calendar to an external link however I already have a rewrite rule in place to redirect sample.com/newdomain/ to newdomain.sample.com/ so when I enter sample.com/newdomain/calendar into the web browser it takes me to newdomain.sample.com/calendar. How can I override the original rewrite I have written to allow sample.com/newdomain/calendar to redirect to an external url.
The rewrite rule that will redirect any url containing /newdomain is:
# Redirect /newdomain to newdomain.sample.com
RewriteRule ^newdomain(/.*)?$ http://newdomain.sample.com$1 [R=301,QSA,L]
Any help is appreciated!

You can place new redirect rule above the previous rule and make sure to clear your browser cache before testing:
RewriteEngine On
RewriteRule ^newdomain/calendar/?$ http://external.site.com/ [R=301,NC,L]
RewriteRule ^newdomain(/.*)?$ http://newdomain.sample.com$1 [R=301,NC,NE,L]

Related

Redirecting some sub pages to another format page using htaccess

I would like to add 301 redirects to specific type subpage on my site which is hosted on LAMP server.
for example,
example.com/witcher-3-100309/player-count - example.com/witcher-3-100309?tab=player-count
example.com/dota-2-100209/player-count - example.com/dota-2-100209?tab=player-count
example.com/pubg-300100/player-count - example.com/pubg-300100?tab=player-count
Is there any way in htaccess to write a general rule for all these type URLs to redirect correctly instead of individual 301 redirect codes in htaccess.
Thanks in advance.
The following should do it
RewriteEngine on
RewriteCond %{THE_REQUEST} /.+\?=([^\s]+) [NC]
RewriteRule ^.+$ %{REQUEST_URI}?tab=%1 [NE,L,R]
This will redirect /foobar/?q=value to /foobar/?tab=value .
When you sure the rule is working ok you can change the R (Temp Redirect flag) to R=301 to make the redirection permanent.

How to rewrite url and redirect with apache mod rewrite?

I have got url:
ipaddress/panelname/main/index.php
How to rebuild it to
ipaddress/center/index.php
?
ofcourse we can see another pages, not only index.php, but this folders in url we can see forever.
I tryed to do this in .htaccess:
RewriteEngine on
RewriteRule ^center/([^/]+)/?$ panelname/main/$1 [L]
RewriteRule ^/panelname(.*)$ /center$1 [QSA,L,R=301,NC]
Redirect 301 ^/panelname(.*)$ /center$1
but i don't see redirect from panelname to center.
but if i type center all works good (but i don't shure, that it works good by my htaccess or by symlink, which i was created in filesystem)
How to rewrite all to another links and howto see redirect from old links to my new? Thank you.
RewriteRule in directory context (which .htaccess is), does never begin with a slash, because the common prefix is stripped from the matched portion first.
Redirect does match strings, not regex'es. The variant that works on a regex is RedirectMatch. Both only work on absolute URL's (the one beginning with a slash).
You either have to do the following:
RewriteRule ^panelname(.*)$ /center$1 [R,L]
or:
RedirectMatch 302 ^/panelname(.*)$ /center$1
Change [R] to [R=301] once you have tested that EVERYTHING works. If you choose the second option, only change 302 to 301 after testing that everything works.
If you want to show /center/index.php to your visitors and keep a redirect from old URL to this URL then you will need one redirect and one rewrite rule (that you already have).
RewriteEngine on
# external redirect from old URL to new one
RewriteCond %{THE_REQUEST} /panelname/main/(\S+) [NC]
RewriteRule ^ /center/%1 [R=302,L]
# internal forward from new URL to actual one
RewriteRule ^center/([^/]+)/?$ panelname/main/$1 [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.

redirect all URLs to domain root with htaccess rewrite

I am trying to get all requests including get variables to redirect to another domain with an htaccess rewrite,
I am trying to redirect all URLs from from my old blog to http://www.passiveincomeidea.com domain root but any URLs with a variable are still passing the variable to the new domain, like this one for example http://mika.lepisto.com/?p=2223
Here is what I have currently:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ http://www.passiveincomeidea.com/ [L,R=301]
How can I get the rewrite to strip all the variables during the redirect?
Thank you!
Append a question mark at the end of redirected URL:
RewriteRule ^(.*)$ http://www.passiveincomeidea.com/? [L,R=301]
For documentation, search for "Modifying the Query String" in the mod_rewrite documentation.

Rewrite friendly URL not work by click on link

I'm trying to rewrite my website URL to more friendly URL, for example:
www.mysite.com/profile.php
to
www.mysite.com/profile
so far I'm able to do it by writing the rewrite URL in the URL address bar, but when go to the page by click on the link, the profile page URL doesn't change to rewrite URL. Am I missing something here?
this is my .htaccess codes
Options +FollowSymLinks
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^profile/?$ profile.php [NC,L] # Handle requests for "profile"
Thanks for any suggestions.
You need to change the URL of profile..
Profile
mod_rewrite will not change URLs in your code. You have to do it.
If you can't or don't want to, you can use RewriteRule with R option to map from old to new URL.
It doesn't change because your rule only internally rewrites profile to profile.php, you need a different rule to do it the other way, and externally so the browser gets redirected:
RewriteCond %{THE_REQUEST} \ /profile\.php
RewriteRule ^ /profile [L,R]