Redirect, but preserve part of URL - .htaccess

its possible create a redirect, replace only part in the URL?
another part preserve...
in this case ONLY listing to business
EXAMPLE:
www.example.com/listing/new-york
www.example.com/listing/boston
www.example.com/listing/etc...
to
www.example.com/business/new-york
www.example.com/business/boston
www.example.com/business/etc...
thank you for tips :)

This is fairly easy to do with mod_rewrite. See this question if you need help enabling that.
Then add the following to your .htaccess
RewriteEngine on
RewriteRule ^listing/(.*)$ business/$1 [R,L]
Change the [R] flag to [R=301] after testing the redirect works as expected to turn the redirect into a permanent redirect.

Related

.htaccess rewrite conflicting rules

I'm having an issue with some htaccess rules which I thought would be simple. I have some nice SEO friendly URL rewriting in place as below
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(index\.php|/images|/templates|/views|/ajax|/uploads|/robots\.txt|/sitemap\.xml|/favicon\.ico|/scripts|/cron|/combine.php|/js|/css)
RewriteRule ^(.*)$ index.php?ref=$1&%{QUERY_STRING} [L]
This all works well and I want to keep this. I also wish to rewrite some old pages which Google WMT is reporting as 404's to the new equivalent and for that I'd like to use:
Redirect 301 /about_us http://example.com/about-us
The problem I have is that the URL that the browser is directed to is:
http://example.com/about-us?ref=about_us
The about_us is the old link and about-us is the correct link. If the htaccess redirected to example.com/about-us then the other SEO friendly rewrite rule will pick it up and show the page but eh extra ?ref= parameter is confusing it. I am guessing the two rules are conflicting to a degree but is there a way to get the two rules to work together e.g. redirect without the extra ?ref= parameter? My knowledge of htaccess is basic to say the least so I am a little stuck on this one.
Thanks in advance
Redirect and RedirectMatch are part of mod_alias, while the rewrite rules are part of mod_rewrite. The problem you're running into is when you mix the two, both modules affect the same request, thus two things happen when you only want one. In this case, you need to stick with just mod_rewrite and use this instead:
RewriteEngine On
RewriteRule ^about_us /about-us [L,R=301]
RewriteCond %{REQUEST_URI} !^(index\.php|/images|/templates|/views|/ajax|/uploads|/robots\.txt|/sitemap\.xml|/favicon\.ico|/scripts|/cron|/combine.php|/js|/css)
RewriteRule ^(.*)$ index.php?ref=$1&%{QUERY_STRING} [L]
Note that the rule that redirects comes before the rule that routes to index.php.

What to do with Question Marks in HTAccess Redirects

I'm new to redirects and have researched how to do this but am just getting more and more confused.
What I'm wanting to do is create a rule to redirect:
From: http://www.example.com/wordpress/?p=1250
To: http://www.otherexample.com/blog
Where I'm running into issues is with the question mark. I've read somewhere that I'm not sure I'm doing this correctly, but here's what I have so far:
**Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^p=1297$
RewriteRule ^wordpress$ linktoothersite [R=301, QSA, L, NC]**
I'm failing somewhere, any of you see what I'm doing wrong? Any help would be appreciated. I could only post two links, so link to other site would be the link.
If it's a single link you want to change you don't need a htaccess rule for that you can just do a redirect.
redirect 301 /wordpress/?p=250 http://www.othersite.com/blog
If the URL parameter changes you can use this rule.
RewriteEngine on
RewriteRule ^wordpress/(.*) http://www.othersite.com/blog [R=301,QSA,NC,L]
Note that when you use the QSA flag it will append the URL with the query parameter so if that's what you want the resulting URL will be.
http://www.othersite.com/blog?p=250
Otherwise if you remove the QSA flag, then it will just redirect to this
http://www.othersite.com/blog

General rulefor redirect in htaccess

I want to make some redirects in htaccess. I will explain in detail what I'm trying to do and at the end I will tell you my question, so if you don't want lots of explains just scroll down.
So for you to understand what I am doing, here is an example: if I enter www.mysite.com/oldpage it should redirect me to www.mysite.com/new page. For this I used:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
Redirect /oldpages http://www.mysite.com/newpages
The problem is that I want to use this for more than one page. So I want that:
mysite.com/oldpage/firstarticle
to redirect to
mysite.com/newpage/firstarticle
And the same with all articles. I want to redirect all like:
Redirect /oldpages http://www.mysite.com/newpages
Redirect /oldpages/firstarticle http://www.mysite.com/newpage/firstarticle
Redirect /oldpages/secondarticle http://www.mysite.com/newpage/secondarticle
But I have a conflict because Redirect /oldpage works on every case.
That's why (AND HERE IS WHAT I DON'T HOW TO DO!!!) I need to use a general rule like:
Redirect /oldpage/(*) http://mysite.com/newpage/(*)
Redirect /oldpage/(any numeric characters-don't need them)/(*) http://mysite.com/NEWSINGLEPAGE/(*)
Instead of (*) it should be any characters,but use them in the new link.
Does someone know how to make this Redirect for more than one page?
Thank you for your time!
!!!!
I DON'T KNOW IF IT'S POSSIBLE BUT IT WOULD BE NICE IF I COULD USE AN IF STATEMENT IN HTACCESS...SOMETHING LIKE:
if (adress is /redirect) Redirect to newpage.html;
else if (adress is /redirect/(some number)/(.*)) Redirect to NEWSINGLEPAGE/$1.html
else if (adress is /redirect/(.*)) Redirect to newpage/$1.html
Change
Redirect /oldpage/(*) http://mysite.com/newpage/(*)
To
RedirectMatch /oldpage/(*) http://mysite.com/newpage/$1
try this
RewriteRule ^oldpage/(.*)$ /newpage/$1 [R=301,L]

How to write .htaccess 301 redirect rule for

I need to redirect some urls.
I need to direct www.site.com/city-st-key-word-string to www.site.com/city-st/key-word-string.
I have about 500 cities I need to do this for. Preferably instead of making a redirect rule, I would like to change the keyword string as well. However, I will end up having 2,500 redirects.
Is it ok to have $2,500 redirects or should I use a redirect rule and if so, what would it be?
Thanks for your help in advance!!!!
Would this keyword string be the same for all cities? If yes, then you could create one rule and change the keyword string in the rule (and it would reflect on all redirects).
Individual rewrite rules would look like this -
Redirect /city-st-key-word-string http://www.site.com/city-st/key-word-string
A single rewrite rule for any city-st would look like this -
RewriteEngine on
RewriteRule ^(.*)/([a-zA-Z]+)-key-word-string$ $1/$2/key-word-string [R=301,L]
A rewrite rule redirecting any /x-x-??? to /x-x/??? (i,e; splitting at the two hyphens) -
RewriteEngine on
RewriteRule ^(.*)/([a-zA-Z]+)-([a-zA-Z]+)-(.*)$ $1/$2-$3/$4 [R=301,L]
Hope this helped :)

htaccess redirect with dynamic variables conflict

I'm working in an old CMS that uses htaccess to rewrite URIs with GET variables into something more user friendly.
RewriteEngine on
RewriteRule ^animals/(.*)/ secondary.php?page=$1
RewriteRule ^animals/(.*) secondary.php?page=$1
which results (correctly) in
http://www.example.com/animals/duck
The problem is I now need to redirect some of those pages to new pages. I've tried:
Redirect 301 /animals/goose http://www.example.com/animals/fowl
The redirect almost works, but it adds "?page=goose" to the end of the rewritten URI:
http://www.example.com/animals/fowl?page=goose
I've tried using RewriteRule as well as RewriteCond, but unfortunatley I'm having no luck. Any help would be immensely appreciated.
Try placing this before the other rules instead of the Redirect statement. R=301 is for the redirect and L signals that the rule in question is the last rule to be processed.
RewriteRule ^animals/goose /animals/fowl [R=301,L]
Also you can easily make the slash (just like any other character) optional with a question mark, instead of having two rules.
RewriteRule ^animals/(.*)/?$ secondary.php?page=$1

Resources