First RewriteRule is being ignored - .htaccess

I am trying to create a Rewrite rule that detects if the request is coming from the user agent and process the URL and if not redirect to the index page. The First Rule does not seem to work and it goes directly to the second rule. I am not sure what I am doing wrong I added the flag [L] to stop the Rule after the condition has been met. It seems that the condition of the first rule is not being met, not certain where to start.
RewriteCond %{HTTP_USER_AGENT} googlebot
RewriteRule ^/(.*)$ / [L]
RewriteCond %{REQUEST_URI} !^/resources/.*$
RewriteCond %{SERVLET_PATH} !-f
RewriteRule ^/(.*)$ /index.html [L]

Could you please try following based on your shown samples only. Please make sure to clear browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_USER_AGENT} googlebot [NC]
RewriteRule ^ / [L]
RewriteCond %{REQUEST_URI} !^/resources [NC]
RewriteCond %{SERVLET_PATH} !-f
RewriteRule ^(.*)/?$ index.html [L]

Related

Removing id/variable from SEO friendly URL

I am trying to rewrite a URL so that it is SEO friendly and excludes the product ID. Currently I am using this in my .htaccess file:
RewriteRule ^shop/product/([^/\.]+)/([^/\.]+)$ products.php?id=$2&name=$1 [NC,L]
For example rewrites it to:
shop/product/product-name/12
Is there a way that I can rewrite this URL so it removes or excludes the product ID from the end?
Could you please try following, written and tested with shown samples. Please make sure you clear your browser cache before testing your URLs.
First set of Rules is considering that your product.php is present in root, in same path as .htaccess is present.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^shop/product/([^/]*)/([^/]*)/?$ /products.php?id=$2&name=$1 [NE,R=301,NC]
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{THE_REQUEST} \s/products\.php\?id=[^&]*&name=[^\s]*\s [NC]
RewriteRule ^(.*)$ shop/product [R=301,QSD,NE,L]
OR Please use either above OR following rules at a time only in your .htaccess.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^shop/product/([^/]*)/([^/]*)/?$ products.php?id=$2&name=$1 [NE,R=301,NC]
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{THE_REQUEST} products\.php\?id=[^&]*&name=[^\s]*\s [NC]
RewriteRule ^(.*)$ shop/product [R=301,QSD,NE,L]

RewriteEngine Adds relative path

I have such rewrite rules
RewriteEngine On
RewriteCond %{HTTP_HOST} ^monkey.pl(.*) [NC]
RewriteRule ^(.*)$ http://www.monkey.pl/$1 [R=301,L]
RewriteRule ^horse.html$ /dog.html
and when I go to the monkey.pl/horse.html I get the message:
The requested URL /home/login/monkey/dog.html was not found on this server.
How can I get this to work. Basically what I'm trying to do is to change address of urls like:
http://www.monkey.pl/produkty.php?strona=1
to be displayed as
http://www.monkey.pl/produkty/czesci_do_mixokretow.html
but none of my rules are working. Therefore I'm trying to come with solution.
I tried many varations and I couldn't get it to work. I don't want to rewrite whole page. Just 6 pages which I need to change url and that's all. Fixed translation url => url.
If you are only doing a handful of URLs then you can do them this way.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} /+produkty\.php\?strona=1 [NC]
RewriteRule ^ /produkty/czesci_do_mixo‌​kretow\.html [R=302,L]
RewriteRule ^produkty/czesci_do_mixo‌​kretow\.html$ /produkty.php?strona=1 [L]
RewriteRule ^horse\.html$ /dog.html [L]

Rewrite rule htaccess disturbing other rewrite rules

I have a page on my website that is getting generated dynamically to list all outlets based on cityf parameter and below is rewrite rule to convert it into SEO friendly URL and it is working pretty well.
RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]
I have a blog page on my website and .htaccess is as below to convert SEO Friendly URL (http://example.com/title-of-blog)
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
Now the problem here i am facing that when someone visits blog page then the link http://example.com/title-of-blog instead of displaying blog detail on the page, displays my Error message that No outlets near title-of-blog.
I got the issue that Apache is not able to identify when to rewrite cityres page and when to rewrite blogdetail page.
Someone suggested that Make sure that each rule has a common prefix (e.g. /blog/page1 and /news/page2). but i did not get that.
Any suggestions here please?
EDIT:
Whole htaccess is as below
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index
RewriteRule ^index\.php$ / [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# remove .php from URL
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
# remove .html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)\.html$ /$1 [L,R=301]
ErrorDocument 404 /error-page
ErrorDocument 403 /error-page
RewriteRule ^food-([^-]*)-([^-]*)\.html$ /pdetail?res_id=$1&location=$2 [L]
RewriteRule ^foodies-([^-]*)-([^-]*)$ /pdetail_new?res_id=$1&location=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /pdetail_ne?location=$1&res_id=$2&name=$3 [L]
RewriteRule ^blog/([^/.]+)/?$ /blogdetail_fm?prmn=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !cityres
RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]
Both your rules match the exact same pattern. Therefore, the first rule will always match and the second rule does nothing.
Looking at the first rule:
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
This matches http://example.com/title-of-blog as well as http://example.com/city-name
When you look at it, you can tell which needs to be handled by blogdetail and which needs to be handled by cityres, but the regex ([^/.]+) sees them both as exactly the same, and matches both. Your regex doesn't know the difference, so whatever the first rule is, both URL's will get matched by it.
Like you said, someone suggested using a prefix. That way, the regex knows which is which:
RewriteRule ^city/([^/.]+)/?$ /cityres?cityf=$1 [L]
RewriteRule ^blog/([^/.]+)/?$ /blogdetail?prmn=$1 [L]
ANd your URLs will look like:
http://example.com/city/city-name
http://example.com/blog/title-of-blog
If you're really hung up about not adding prefixes, you can remove the second prefix:
RewriteRule ^city/([^/.]+)/?$ /cityres?cityf=$1 [L]
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
So that you have:
http://example.com/city/city-name
http://example.com/title-of-blog
EDIT:
Your 500 server error is caused by the rules looping. You need to add a condition so that they won't keep matching:
RewriteRule ^blog/([^/.]+)/?$ /blogdetail?prmn=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !cityres
RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]

Rewrite Rule Adds ? to URL

I'm trying to redirect all URLs to https except ones that start with /feeds/. This is an ExpressionEngine site, so I also have to remove index.php from the beginning of the URLs. The hosting setup also requires that ? be at the end of index.php. Here's what I have:
RewriteEngine On
RewriteBase /
# Force SSL for everything but feeds
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/(index.php\?*/)*feeds/ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$2 [R,L]
# Remove index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
But when I go to http://www.mysite.com/feeds/blog, it gets redirected to https://www.mysite.com/?/feeds/blog.
I don't understand why this is happening. Here's the course of events as I see it:
http://www.mysite.com/feeds/blog is using port 80, so it meets the first condition
http://www.mysite.com/feeds/blog starts with /feeds/, so it fails the next condition and exits the rule
http://www.mysite.com/feeds/blog is not a file or directory, so it meets the next 2 conditions.
http://www.mysite.com/feeds/blog is changed to http://www.mysite.com/index.php?/feeds/blog and loops back to the top.
http://www.mysite.com/index.php?/feeds/blog is using port 80 and it starts with /index.php?/feeds/, so it fails the condition again and exits the rule
http://www.mysite.com/index.php?/feeds/blog does exist, so it fails the next 2 conditions and exits the rule.
The final server-side URL is http://www.mysite.com/index.php?/feeds/blog, and the client-side URL is still http://www.mysite.com/feeds/blog.
What's happening here? Where's that ? segment coming from?
Try this corrected code:
RewriteEngine On
RewriteBase /
# Force SSL for everything but feeds
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !/feeds/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L,NE]
# Remove index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?/$1 [L]

Redirect a subdomain to a dynamically generated folder and mask theurl

I currently have a blog setup as the subfolder of the main site, mydomain.com.au/blog/, however the /blog is dynamically generated through PHP and doesn't physically exist.
I have created a subdomain blog.mydomain.com.au and I am trying to get it to display everything just like mydomain.com.au/blog/ but mask the url so that it shows blog.mydomain.com.au.
I have made several attempts using htaccess and got close a couple of times, but there's always something wrong. Below are the 2 attempts which got me the closest to the expected results:
1st attempt
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com\.au$
RewriteRule ^.*$ http://www.mydomain.com.au/blog%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This will get blog.mydomain.com.au to redirect to www.mydomain.com.au/blog/, but does not mask the URL (the address bar will show www.mydomain.com.au/blog/).
2nd attempt
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com\.au$ [NC]
RewriteRule blog/(.*) http://blog.mydomain.com.au/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This got me the closest. It redirects and masks the URL just the way I want it to for all pages under /blog/ (e.g. blog.mydomain.com.au/whatever/ redirects to www.mydomain.com.au/blog/whatever/ but masks the URL as blog.mydomain.com.au/whatever/), but it doesn't do it for blog.mydomain.com.au, which gets redirected to www.mydomain.com.au.
You have two different requirements here. The first one is a rewrite, which will work, if the request is blog.mydomain.com.au and you remove the domain part and the R flag in the rule substitution
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com\.au$ [NC]
RewriteRule !^blog/ /blog%{REQUEST_URI} [L]
The second one is an actual redirect, if the request is www.mydomain.com.au/blog/
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com\.au$ [NC]
RewriteRule blog/(.*) http://blog.mydomain.com.au/$1 [R,L]
When everything works as you expect, you can change R to R=301.
Never test with 301 enabled, see this answer
Tips for debugging .htaccess rewrite rules
for details.

Resources