On my website for SEO purpose the link to hotel.php?hotel_id=104 is changed to parkgrand.html
and changed htaccess to refer to the same script
code : RewriteRule ^parkgrand\.html$ /hotel.php?hotel_id=104
Till this it works fine. But I want if some user still accesses the old url i.e. hotel.php?hotel_id=104 then he should get automatically redirected to parkgrand.html and user agent should get a response 301
So, the code now becomes
RewriteCond %{REQUEST_URI} ^/hotel.php
RewriteCond %{QUERY_STRING} ^hotel_id=104
RewriteRule ^hotel.php /parkgrand.html [L,R=301]
RewriteRule ^parkgrand\.html$ /hotel.php?hotel_id=104
But this is causing a redirect loop and the intended page doesn't gets displayed to the user.
Can anyone tell me what should be the correct code for this.
Give this a try:
# Redirect /hotel.php?hotel_id=104 to /parkgrand.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+hotel\.php\?hotel_id=104[&\s] [NC]
RewriteRule ^ /parkgrand.html? [R=302,L]
Change from R=302 to R=301 once you confirm it is working to avoid caching.
Keep in mind you may have been cached from your previous attempts since you're using R=301, I will recommend you to test the above using a different browser to make sure its working in case your usual browser is cached.
Related
Trying to ask my question again and explain it the best I can.
I am running WAMP on windows 10. Apache version 2.4.18. My project is in www folder named hoidja.ee. mod_rewrite is enabled, AllowOverrideis set to All.
Inside that folder I have .htaccess file, where I am trying to accomplish rewrite without redirect and it does not work at all. I know the .htaccess file is working, since I can rewrite index.php to /home/ for example and it is working perfectly fine.
I have a page with all the users listing. You can click on the user, and it will take you to a url:
http://localhost/Hoidja.ee/hoidja.php?user_id=94&username=John
How I would like this to show is
http://localhost/Hoidja.ee/user/John
without any redirection.
What I have tried:
#tried first with only user_id
RewriteCond %{QUERY_STRING} ^user_id=([A-Za-z0-9-]+)$
RewriteRule ^hoidja\.php$ /user/%1? [L]
This gave me 404 that /user/94 doesn't exist.
RewriteRule ^user/([0-9]+)/([A-Za-z0-9-]+)/?$ hoidja.php?user_id=$1&username=$2 [QSA]
This doesn't do anything visually. If I enter http://localhost/Hoidja.ee/user/94/John in the url manually, it goes to that page and shows the user data, but if I enter the original url it does not rewrite it. I am using <base> as well.
I have tried all the possible ways but nothing seems to work. How can I accomplish the url rewrite?
You are getting the 404 error because you are rewriting to a non-existent path /user/94 .you will need to redirect /hoidja.php?user_id= to /user/94 and then rewrite /user/94 back to the real location.
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^user_id=([A-Za-z0-9-]+)$
RewriteRule hoidja\.php$ /hoidja.ee/user/%1? [L,R]
RewriteRule ^(?:hoidja\.ee/)?user/(.+)$ /hoidja.ee/hoidja.php?user_id=$1 [L]
I have a website where all URLs are rewritten to avoid seeing extensions and the original file name. So, in my htaccess, I have the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^example-1$ filename_1.php
RewriteRule ^example-2$ filename_2.php
Now, I also have a few pages that I wanted to be only https and only http. So, let's say I want filename_1 to alwaysbe https and filename_2 to always be http. So, I have the following codes.
RewriteCond %{HTTPS} off
RewriteRule ^(filename_1)\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R]
And
RewriteCond %{HTTPS} on
RewriteRule ^(filename_2))\.php$ http://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R]
This is just and example but I actually have many files affected by those two rules.
Now, I thought it was working great until I noticed that when I am on an https page and I access a non https page, the url changes to show the actual file name. So, if I am on https://www.mywebsite.com/example-1 and I try to access example-2, the url changes to http://www.mywebsite.com/filename_2.php instead of http://www.mywebsite.com/example-2. And the same for going from http to https. (I am keeping it simple in those examples but the problem only arise with pages with variables in the url. Simple ones like www.mywebpage.com/homepage work just fine.)
So, I added this line of code:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(example-2)/ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301,NE]
So, it worked to go from https to http but then things just got crazy! I get redirect loops at certain https and non https page and I have no clue why! The thing is that I started by adding one page at a time to this new rule. And pages that I haven't even added yet give me some redirect loop. Then I try adding those pages but instead of https on, I used https off and it got even crazier.
So my question is what is a good way to have all the following perform harmoniously:
1. Rewrite all urls to custom ones
2. Having http and https page while keeping the custom urls and avoiding all redirect loops.
Thanks!
Have a page currently with the URL /results-details.php?mls_number=stringofnumbers.
I want it to re-write to: /results-details/stringofnumbers
However I want that to basically resolve back to the original page.
I'm also changing all the URLs on the site internally to point to the URL. So I have two re-write rules:
RewriteCond %{QUERY_STRING} mls_number=([0-9]+)$
RewriteRule (.*) /new-homes/results-details/%1? [R=301,C]
RewriteRule ^results-details/([0-9]+)$ results-details.php?mls_number=$1
The second rule works fine on it's own with internal links in the /results-details/stringofnumbers form, but the first one doesn't chain properly to the second one and not sure what I'm doing wrong.
Basically trying to retain any links to the old URLs that might be out there but start using the new URS internally.
Suggestions?
YOu need to match against the actual request and not the URI, because the rewrite engine loops:
RewriteCond %{THE_REQUEST} \ /new-homes/results-details\.php?mls_number=([0-9]+)
RewriteRule ^ /new-homes/results-details/%1? [L,R=301]
RewriteRule ^results-details/([0-9]+)$ results-details.php?mls_number=$1
I am having an issue where Google Webmaster Tools is reporting a ton of 404 links to my site which are coming from ask.com.
I have tried to get ask.com to fix their side but of course they are not, so now I am stuck with over 11k of bad links to my site which I am suspecting is effecting my ranks right now.
Anyways I have a possible way to 301 them, but not sure how to do it with .htaccess.
Here is the bad link pointing to my site
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/about.php
It should be
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/
Besides the about.php there are other variations of endings as well, I basically need to be able to remove the ending.
Problem is that the URL after /sentence-examples/ can change. The beginning is always:
http://www.freescrabbledictionary.com/sentence-examples/
So basically:
http://www.freescrabbledictionary.com/sentence-examples/<-keep but can change->/<-keep but can change->/<-remove this->
This .htaccess should be placed on the folder before sentence-examples:
RewriteEngine on
# Redirect /sentence-examples/anything/anything/remove to /sentence-examples/anything/anything/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(sentence-examples/[^/]+/[^/]+)/.* [NC]
RewriteRule ^ /%1/? [R=302,PT,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ /sentence-examples/examplesentence.php?havethis=$1&word=$2 [L]
Change 302 to 301 once you confirm it's working as expected.
If you have a CMS installed you might need a different rule to work along with it without conflicting.
Keep in mind that if you had previously tried different redirects using 301 aka permanent redirect its recommended that you use a different browser to test this rule to avoid the caching.
This is possibly quick and dirty but I've done a simple test on localhost and here just to make sure it works.
RewriteEngine On
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)\.php http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]
You can see that I've added wildcard groups (.*) to the RewriteRule so that we can pick up the elements of the URL that we need to aid in proper redirection i.e. $1 and $2. You can also use the third one ($3) to get which destinations are being targeted alot for your SEO needs.
NB: The rule above assumes that that the redirected URL will always be from a .php target and to ensure that you can redirect regardless of whatever comes after the 3rd URL segment replace the RewriteRule with this
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)$ http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]
How to redirect a first time user to a special landing page using htaccess based on referrer? I mean if they came from another domain then they are the first time visitor?
I am really noob at url rewriting and explanation would be great .
Note: the landing page is nothing but a php script that detects browser. On that page I will use cookie, but need to redirect the user if the referrer is empty or its from another domain.
I suggest this :
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^(www\.)?(https?://)?example\.com[NC]
RewriteCond %{REQUEST_URI} !^/welcome.html [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,L]
The first RewriteCond check if referer contains your domain name, and the second check if you are not just redirected by the RewriteRule.
The RewriteRule brings you to the welcome page as a [L]ast rule.
How about redirect the use if his referer is not your domain ?
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^(www\.)?(https?://)?(?!example\.com) [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,NC]
That means that the user will be redirected to welcome.html if he writes example.com in the address bar or comes from a link in another site. Once on your site it won't be redirected anymore if he load another page in your site.
P.S. AFAIK you can use cookies in PHP that generates a plain html page see here
Edit: Update tested code
Excuse my reheating the old steak once more.. I would still be interested in knowing if anyone knows the solution to this problem - without using cookies or HTML5 features...
I have read here that the HTTP_REFERER might be blank. Is that why this method of redirecting is not good for this application? I have experimented with this on my server but the closest result working result was being always redirected to my landing page index.htm, which is not desired..
Could this rule interfere with other rewrite rules?
Also, there is an error in the former snippet:
And I think the NC flag in the latter snippet does not make sense. Should it not be L?
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^(www\.)?(https?://)?example\.com[NC]
#missing space after .com and before [----------------here----^
RewriteCond %{REQUEST_URI} !^/welcome.html [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,L]
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^(www\.)?(https?://)?(?!example\.com) [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,NC]
#Should this flag not be L? ------------------------------^