Conflicting rewrite rules - .htaccess

I have several rewrite rules that are listed below
RewriteRule ^([^/\.]+)\.html?$ php/statePage.php?region_slug=$1 [L]
RewriteRule ^([^/\.]+)-tree-services/([^/\.]+)\.html?$ php/cityPage.php?region_slug=$2&state_slug=$1 [L]
RewriteRule ^([^/\.]+)-tree-services/([^/\.]+)/([^/\.]+)\.html?$ php/vendorPage.php?slug=$3&region_slug=$2&state_slug=$1 [L]
RewriteRule ^([^/\.]+)-Sitemap\.xml?$ php/xmlState.php?region_name=$1 [L]
RewriteRule ^([^/\.]+)\.html?$ php/otherPages.php?pageSlug=$1 [L]
The first four work properly however the last rewrite rule displays the template for the statePage.php file not the otherPages.php file.

Related

.htaccess wild card redirect myweb.com/maindir/subdir/abcd.php to abcd.php?param=123

I am new to .htaccess
I am using apache rewrite to give access to virtual directories
following code works
RewriteRule ^maindir/subdir/prog1.php?$ prog1.php?schoolid=12 [L]
RewriteRule ^maindir/subdir/prog2.php?$ prog2.php?schoolid=12 [L]
RewriteRule ^maindir/subdir/prog3.php?$ prog3.php?schoolid=12 [L]
RewriteRule ^maindir/subdir/prog4.php?$ prog4.php?schoolid=12 [L]
RewriteRule ^maindir/subdir/prog5.php?$ prog5.php?schoolid=12 [L]
RewriteRule ^maindir/subdir/prog6.php?$ prog6.php?schoolid=12 [L]
but,
Instead of writing htacess code for each program prog1.php, prog2.php etc
can i use wild card...
Need your help
so that
https://abcd.com/maindir/subdir/anyprogram.com
will be actually executes
https://abcd.com/anyprogram.com
I tried (so as to use wild card in htaccess)
RewriteRule ^maindir/subdir/(.*)$ 1?schoolid=12 [R,L]
RewriteRule ^maindir/subdir/? https://abcd.com/?schoolid=12 [L,NE,R=301]
RewriteRule ^maindir/subdir/?$ $1?schoolid=12 [L,NE,R=301]
but failed

Rewrite rule in htaccess is not working

Here are the .htaccess rules:
RewriteEngine On
RewriteRule ^([^/]*)$ content.php?seourl=$1 [L]
RewriteRule ^pdf/([^/]*)$ content-single.php?seourl=$1 [L]
RewriteRule ^browsepdf$ browse.php [L]
RewriteRule ^browsepdf-([^/]*)$ browse.php?page=$1 [L]
RewriteRule ^download/([^/]*)$ download.php?pdf=$1 [L]
RewriteRule ^sitemap.xml$ xmlsitemap.php [L]
Options -Indexes
Here every URL is pointing to content.php?seourl=$1, even css, js and image files.
Here are some example URLs I need,
http://example.com/sjskjfsk21
http://example.com/asfasfasf43sf
http://example.com/pdf/fhfdhdh3432aaf
http://example.com/pdf/aisfyiahm2faf3
http://example.com/browsepdf
http://example.com/browsepdf-1
http://example.com/browsepdf-2
http://example.com/download/fjaskfjalsk3rs
http://example.com/download/usaydiy7aisydi
http://example.com/sitemap.xml
Can anyone please fix the .htaccess file.
Here every url is pointing to "content.php?seourl=$1"
Because your first (generic) rule catches all the requests. You need to change the order so you have the most specific rules first, and the most generic (catch-all) rules at the end. In your case you just need to move the first rule to the end. For example:
RewriteRule ^pdf/([^/]*)$ content-single.php?seourl=$1 [L]
RewriteRule ^browsepdf$ browse.php [L]
RewriteRule ^browsepdf-([^/]*)$ browse.php?page=$1 [L]
RewriteRule ^download/([^/]*)$ download.php?pdf=$1 [L]
RewriteRule ^sitemap\.xml$ xmlsitemap.php [L]
RewriteRule ^([^/]*)$ content.php?seourl=$1 [L]
NB: I backslash-escaped the dot in sitemap.xml to match a literal dot, otherwise it matches any character.
even css, js and image files.
You can make an exception for these static resources at the beginning of your file, before the existing directives. For example:
RewriteRule \.(js|css|png|jpg|gif)$ - [L]
For any URL that ends in any of the stated file extensions then stop processing the current mod_rewrite rules.
Alternatively (although perhaps marginally less efficient), you can prevent processing of requests for files that exist. Again, this goes before your existing mod_rewrite directives. For example:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
However, this must now check every request for the existence of a file on the filesystem that maps to the request. (It could also be combined with the above rule if required.)
UPDATE: Bringing this together, we have:
# Exclude any existing files from being rewritten
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Specific rewrites
RewriteRule ^pdf/([^/]*)$ content-single.php?seourl=$1 [L]
RewriteRule ^browsepdf$ browse.php [L]
RewriteRule ^browsepdf-([^/]*)$ browse.php?page=$1 [L]
RewriteRule ^download/([^/]*)$ download.php?pdf=$1 [L]
RewriteRule ^sitemap\.xml$ xmlsitemap.php [L]
# Any other requests for the form "/<anything>"
RewriteRule ^([^/]*)$ content.php?seourl=$1 [L]

htaccess similar file names goes to same page

Most of the RewriteRules are working fine but there are a couple that have the same word in them and aren't going to the correct page.
Here is my full htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
# Rewrites for main pages
RewriteRule home index.php
RewriteRule about-us about-us.php
RewriteRule business-advice business-advice.php
RewriteRule associates associates.php
RewriteRule become-an-associate associates-sign-up.php
RewriteRule blog blog.php
RewriteRule contact-us contact-us.php
RewriteRule log-in log-in.php
RewriteRule sign-up sign-up.php
The problem resides within the two associated links. When I go to [MYURL]/associates it works fine but if I go to [MYURL]/become-an-associate it takes me to the correct URL but shows content from [MYURL]/associates
Anybody got any ideas?
Thanks,
The pattern for RewriteRule's are regular expressions and the rules all loop until the URI stops changing. That means the first time around, when you request /become-an-associate, it matches and is rewritten to /associates-sign-up.php. Then, the second time around, the rule with the pattern associates matches because of "/associates-sign-up.php". You need to add boundary checks (e.g. ^ and $) as well as the [L] flag:
# Rewrites for main pages
RewriteRule ^home/?$ index.php [L]
RewriteRule ^about-us/?$ about-us.php [L]
RewriteRule ^business-advice/?$ business-advice.php [L]
RewriteRule ^associates/?$ associates.php [L]
RewriteRule ^become-an-associate/?$ associates-sign-up.php [L]
RewriteRule ^blog/?$ blog.php [L]
RewriteRule ^contact-us/?$ contact-us.php [L]
RewriteRule ^log-in/?$ log-in.php [L]
RewriteRule ^sign-up/?$ sign-up.php [L]

301redirect to remove folder from URL

I have:
mydomain.com/folder-name/segment1/segment2
I want to change it to:
mydomain.com/segment1/segment2
using a 301 redirect.
I've tried:
RewriteCond %{REQUEST_URI} !^/test/.*$
RewriteRule ^(.*)$ /test/$1 [L]
but its not working
here is my htacess file:
# #AddHandler application/x-httpd-php53 .php .php5 .php4 .php3
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/b1/.*$
RewriteRule ^(.*)$ /b1/$1 [R=301,L]
The answer for the first part of the question should be like this:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/? $2/$3 [R=301,L]
The second code that you've tried is the opposite of what you're asking for initially. This line matches anything not starting with /test/:
RewriteCond %{REQUEST_URI} !^/test/.*$
This line says take everything and rewrite it to the /test/ directory:
RewriteRule ^(.*)$ /test/$1 [L]
So together anything that's not in the test directory is being rewritten to the test directory.
If you're trying to specifically remove the word test then you would remove the ! symbol in your attempt to create a match. Since you already know it's called test there's no need to even make Apache perform this look for 'test' because Apache handles the RewriteCond statement after the RewriteRule (rather unintuitively).
RewriteCond %{REQUEST_URI} ^/?test
You can specialize the rewrite rule like this (I've added [QSA] to catch any query strings:
RewriteRule ^test/([^/]+)/([^/]+)/? $1/$2/ [R=301,L,QSA]
Simply change your code to:
RewriteRule ^test/(.*)$ /$1 [R=301,L,NC]

.htaccess Rewrite all

I have the following rules in .htaccess. Unfortunately, it does not work due to the last rule (everything else works fine). Why?
Options -Indexes
RewriteEngine On
RewriteRule ^(cdn) - [L]
RewriteRule ^admin/(.*)$ backend_0.0.1/index.php/$1 [QSA,L]
RewriteRule ^css/(.*)$ frontend_0.0.1/css.php/$1 [NC,QSA,E=no-gzip:1,L]
RewriteRule ^js/(.*)$ frontend_0.0.1/js.php/$1 [NC,QSA,E=no-gzip:1,L]
RewriteRule ^(.*)$ frontend_0.0.1/index.php/$1 [QSA,L]
If I replace the last line by:
RewriteRule ^(.*)$ frontend_0.0.1/index.php?q=$1 [QSA,L]
Then it suddenly starts to work but previous rules are skipped and only this last rule is applied. But I need rules to stop rewriting once the first one mathches.
You need to exclude the destinations you are redirecting to:
RewriteCond $1 !^(backend_0\.0\.1|frontend_0\.0\.1)/
RewriteRule ^(.*)$ frontend_0.0.1/index.php/$1 [QSA,L]

Resources