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]
Related
I have a blog page on my website and .htaccess is as below to convert SEO Friendly URL
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
So, my URL becomes http://example.com/title-of-blog and it is working pretty fine and awesome but my client reported today that admin section is not working properly and i checked and found that URL http://example.com/admin is getting converted to http://example.com/admin?prmn=admin because of above rule.
I googled how to exclude admin folder from Rewrite then i got below
RewriteRule ^admin($|/) - [L]
When i use this then when i type http://example.com/admin then m getting error page that i defined in htaccess.
Please help. My Full Htaccess is as below
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^admin($|/) - [L]
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]
# restrict .php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[^\ ]+\.php($|\ )
RewriteRule \.php$ / [F,L]
# remove .html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)\.html$ /$1 [L,R=301]
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
ErrorDocument 404 /error-page
ErrorDocument 403 /error-page
ErrorDocument 500 /error-page
I suppose there is an other rule that does something with /admin, but because you are using that second rule it will never reach that rule. Use a condition to exclude anything starting with admin instead. Also please note that the rule would rewrite it to /blogdetail?prmn=admin, so somewhere something is rewritten more.
RewriteCond %{REQUEST_URI} !^/(admin|blogdetail)
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
Here is the what I'm trying to do. I've got these urls, Old form and New form
site.com/video.php -> site.com/video
site.com/index.php?cat_id=1 -> site.com/some-cat-name
Here is my htaccess file which is working only if one of the last two is commented.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^\.htaccess$ - [F]
RewriteCond $1 !^(index\.php|img|js|css|admin|robots\.txt)
RewriteRule ^video$ /videos.php/$1 [L]
#RewriteRule ^(.*)$ /index.php/$1 [L]
if those two are active the server returns error 500
RewriteRule ^video$ /videos.php/$1 [L]
RewriteRule ^(.*)$ /index.php/$1 [L]
my logic is if first is matched video in the url redirect to videos.php and forget for all under this line, but seems to be in other way. Can some one to explain how to have some predifined urls and all others to go on index.php, tnx in advanced.
You are getting 500 (Internal server error) because your rule is looping in this rule:
RewriteRule ^(.*)$ /index.php/$1 [L]
To fix this rule have your .htaccess like this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|img|js|css|admin|robots\.txt)
RewriteRule ^video$ /videos.php/$1 [L]
# ignore existing files and directories from rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
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]
I have searched for this but nobody have the same issue..
Now I need an code to transform the ugly url (thats what i have) to the nice url:
Ugly url: domain.com/aanmelden?referral=admin
Nice url: domain.com/aanmelden/admin
Ik have tried soo many codes, but no one did work for me. ):
suggestions?
Thnx!
(My current htaccess: )
ErrorDocument 401 /errordocs/401.php
ErrorDocument 403 /errordocs/403.php
ErrorDocument 404 /errordocs/404.php
ErrorDocument 500 /errordocs/500.php
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^domain.nl$ [NC]
RewriteRule ^(.*)$ http://domain.nl/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]
Where and what i need to place?
EDIT:
To be clear:
If the user goes to the Nice Url, they wil stay on the nice one.
If the user goes to the Ugly Url, they wil redirect to the Nice url.
This is a trivial rewrite:
If admin can be any lowercase string, use the pattern ([a-z]+)$ to capture one or more lowercase letters after the / and before the end of the request URI.
RewriteEngine On
RewriteRule ^aanmelden/([a-z]+)$ aanmelden?referral=$1 [L]
If admin is really only admin, you can hard-code it as
RewriteEngine On
RewriteRule ^aanmelden/admin aanmelden?referral=admin [L]
Update
In context of your existing rewrites just posted, you'll need to add this rule before the rule that adds .php since that would also match this pattern.
# Do this before the rule that adds .php
# Also added condition so this doesn't apply to real files...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^aanmelden/([a-z]+)$ aanmelden?referral=$1 [L]
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Update 2
If you want the end user to get the ugly URL and rewrite it to the nice one, use:
RewriteCond %{REQUEST_QUERYSTRING} referral=([a-z]+)
RewriteRule ^aanmelden aanmelden/%1 [L]
First off I am using the Codeigniter Framework so this issue is a workaround the way CI process URLs along with the current redirects I have set up using mod_rewrite.
I am trying to get a URL like this /?gclid=somestringgoeshere to redirect to /index.php?/home/gclid/somestringgoeshere.
The current .htaccess I have set is below
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6_$7 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4 [L]
RewriteRule ^([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3 [L]
RewriteRule ^([^_]+)-([^.]+)$ index.php?/$1_$2 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# This last condition enables access to the images and css folders, and the robots.txt file
# Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I am trying to use the following code right above the first set up rewrite conditions and rule's to catch it before it try's anything else
RewriteRule ^?gclid=(.*)$ index.php?/home/gclid/$1 [L]
and
RewriteRule ^\?gclid=(.*)$ index.php?/home/gclid/$1 [L]
and
RewriteRule ^/?gclid=(.*)$ index.php?/home/gclid/$1 [L]
All either don't show the correct page or come up with a 500 internal error.
The URI’s query can only be tested with the RewriteCond directive:
RewriteCond %{QUERY_STRING} ^gclid=(.*)
RewriteRule ^$ index.php?/home/gclid/%1 [L]
Or more general (will consider further query parameters):
RewriteCond %{QUERY_STRING} ^([^&]*&)*gclid=([^&]*)
RewriteRule ^$ index.php?/home/gclid/%2 [L]
Oh, by the way: RewriteCond directives only correspond to the first following RewriteRule directive.