Htaccess rewrite rules trouble - .htaccess

I am having troubles getting my rewrite rules to work correctly..
ErrorDocument 404 /404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^companies/
RewriteRule ^companies\/\?(.*)([A-Za-z]+) index.php?cpy=$1 [R=301,L,NC]
RewriteRule ^([0-9]+)/?$ index.php?cid=$1 [L]
RewriteRule ^([A-Za-z_]+)/?$ index.php?cat=$1 [L]
The rewrite rules for
domain.com/123
domain.com/abc
seem to work ok,
but the other one I cannot get to work is
domain.com/companies/?list=this
It seems that apache doesn't find a match for
RewriteRule ^companies\/\?(.*)([A-Za-z]+) index.php?cpy=$1 [R=301,L,NC]
Can anyone help me figure out what is wrong with that rule? or if it is something else?

RewriteRules do not match on query strings as a rule.
You can match on them using RewriteCond with the parameterized matches appearing as %1, %2, etc.
eg.
RewriteCond %{QUERY_STRING} ^(.*)([A-Za-z]+)$
RewriteRule ^companies\/ index.php?cpy=%1 [R=301,L,NC]

Related

RewriteRule that overrides another RewriteRule or at least be more specific

I build a website with multiple languages, and until now I did a simple rewrite for internal pages.
RewriteRule ^([^/]+)$ /page.php?id=$1 [L]
So all my pages are like this:
website.com/about
But now I added another rule, for multiple languages
RewriteRule ^/?(en|es|ar|fr|de|ru|pt-br)/(.+?)/?$ /$2&lang=$1 [L]
The problem is, I have more pages, like the index page or profile pages, like this:
website.com/en/
website.com/en/profile.php
both of them are not working anymore because they redirect me to different pages. like
page.php?id=en
page.php?id=profile.php&lang=en
I'm pretty confused on what is the solution for this.
Note that I don't want my main language to have a prefix /he/
This is the entire htaccess
RewriteEngine On
ErrorDocument 404 /error
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^/]+)$ /page.php?id=$1 [L]
RewriteRule ^/?(en|es|ar|fr|de|ru|pt-br)/(.+?)/?$ /$2&lang=$1 [L]
I want these
website.com/
website.com/en/
website.com/en/profile.php
website.com/about
website.com/en/about
to match these
website.com/
website.com/?lang=en
website.com/profile.php?lang=en
website.com/page.php?id=about
website.com/page.php?id=about&lang
Not sure if this is the best way to do this, but it works:
RewriteEngine On
ErrorDocument 404 /error
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(en|es|ar|fr|de|ru|pt-br)/?$ ?lang=$1 [L]
RewriteRule ^(en|es|ar|fr|de|ru|pt-br)/(.*).php /$2.php?lang=$1 [L]
RewriteRule ^(en|es|ar|fr|de|ru|pt-br)/?(.*) /page.php?id=$2&lang=$1 [L]
RewriteRule ^([^/]+)$ /page.php?id=$1 [L]
I tested it with this website

Redirect to anothe page using .htaccess

I want to redirect the url of http://sitename/modules/fb/fb.php to http://sitename/modules/take_control .How can i do that using htaccess
I tried like
RewriteRule ^/?modules/fb/fb\.php$ take_control , Is that right ?
Your solution is the opposite of what you most likely want if you are trying to rewrite to take_control.
Give this a try. These rules should provide the rewrite with take_control as the URI both ways.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /+modules/fb/fb\.php [NC]
RewriteRule ^ /take_control/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^take_control/?$ /modules/fb/fb.php [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]

SEO-friendly URL rewrite in .htaccess

Before you flag this question -- I did search for answers, and found one close to mine that was not answered directly, so...
I am trying to write a simple RewriteRule in my .htaccess file to change ONLY the links that match to an SEO-friendly format.
RewriteEngine On
RewriteRule ^/includes/seo.([a-zA-Z]+).php$ /$1
So if I have a file
/includes/seo.mydocument.php
it will appear in the browser as
/mydocument
Not sure what I'm missing.
Leading slash is not matched in .htaccess rules so use this
RewriteRule ^includes/seo\.([a-zA-Z]+)\.php$ /$1 [L,R]
But probably you want this:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+includes/seo\.([^.]+)\.php[/\s?] [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ /includes/seo.$1.php [L]

Htaccess redirect with query

I'm having an issue with a query string, although I'm not sure what i'm tryign to do is possible.
I have the following url with a search query attached:
subdomain.mysite.com/search/?search=searchquery
but I need it to redirect to the following url including the query string:
subdomain.mysite.com/search/?rs=searchquery
I was wondering if this is possible with a mod_rewrite?
I've tried the following:
RewriteCond %{REQUEST_URI} ^/search/?$
RewriteCond %{QUERY_STRING} ^search=([0-9]*)$
RewriteRule ^(.*)$ http://subdomain.mysite.com/search/?rs=% [L,R=301]
I don't know if my Syntax is incorrect or i'm just barking up the wrong tree. Any help would be awesome.
This is the Start of my HTAccess File - This is a Wordpress site with the rewrite rule provided by user: Amine Hajyoussef:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_URI} ^/search/?$
RewriteCond %{QUERY_STRING} ^search=([^&]*)$
RewriteRule ^(.*)$ /search/?rs=%1 [L,R=301]
</IfModule>
Unfortunately this doesnt appear to work? Any ideas?
you forgot the 1 after %, i have also changed the search string so it can be anything instead of just number.
RewriteCond %{REQUEST_URI} ^/search/?$
RewriteCond %{QUERY_STRING} ^search=([^&]*)$
RewriteRule ^(.*)$ /search/?rs=%1 [L,R=301]

Resources