My .htaccess:
RewriteEngine On
CheckCaseOnly On
CheckSpelling On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^Blog/(.*?)$ /Me/profile.php?username=$1 [QSA,L]
The problem is, when the URL is like this, it works:
localhost/Me/Blog/ExampleUser
But it doesn't work when it is like this (notice the 'b' in 'Blog' is in lowercase?):
localhost/Me/blog/ExampleUser
I'm running it on the new version of XAMPP. It is wierd its not working even though I have the mod_speling.so on the PHP config.
What is the problem?
Use of the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner. That is, it doesn't care whether letters appear as upper-case or lower-case in the matched URI.
Try [QSA,L,NC] instead, so the comparison is made in a case-insensitive manner
the problem is that rewrite rules ARE case sensitive. So your Rewrite rule should be:
RewriteRule ^[Bb]log/(.*?)$ /Me/profile.php?username=$1 [QSA,L]
and voila you are fixed.
mod_speling.so has NOTHING to do with this.
Related
I had a rule in my .htaccess that makes URLs for articles more friendly looking for the purposes of SEO and the like.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(news-and-views)/(.+).php /$1/article.php?title=$2 [L]
Which converted this URL:
/news-and-views/going-for-brokering.php
To this within the application itself:
/news-and-views/article.php?title=going-for-brokering
Now I need a URL with an ID before the title like this:
/news-and-views/123456789/going-for-brokering.php
So I tried the following rule:
RewriteRule ^(news-and-views)/(.+)/(.+).php /$1/article.php?Id=$2&title=$3 [L]
However, this isn't working, am I misunderstanding the use of the brackets as I thought everything between them was acknowledged as a variable on the right-hand side?
I'm thinking it could even be that the less specific rule is above the more specific rule.
You need to be careful about the order of the rules, since your first rule will also match /news-and-views/123456789/going-for-brokering.php. Change your rules as follows:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(news-and-views)/([^/]+).php /$1/article.php?title=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(news-and-views)/([0-9]+)/([^/]+).php /$1/article.php?Id=$2&title=$3 [L]
I'm having some problems rewriting URLs with the following rules
RewriteEngine on
RewriteRule ^page/(.*)$ index.php?pag=cms&title=$1 [NC]
RewriteRule ^admin/(.*)$ admin/$1 [NC]
RewriteRule ^(.*)$ index.php?pag=$1 [NC,L]
What I'm trying to achieve is to check if the URL is a cms page or not and leave admin URLs as they are.
If I remove the last condition it works but I will have no rule for not cms pages.
Ideally I would want to have just one rule for every page (cms or not) but I can't figure out how to check that other than using page/ in the URL.
Mod_rewrite will keep looping through all the rules until the URI stops changing (or it reaches its internal redirect limit, resuling in a 500 error). You need to add a few conditions to the last rule so that it won't rewrite URI's that's already been properly routed:
RewriteRule ^page/(.*)$ index.php?pag=cms&title=$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?pag=$1 [NC,L]
Additionally, the second rule does nothing except a passthrough, so you can replace it with
RewriteRule ^admin/(.*)$ - [NC,L]
You need these rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
From the Apache documentation:
'-d' (is directory)
Treats the TestString as a pathname and tests whether or not it exists, and is a directory.
'-f' (is regular file)
Treats the TestString as a pathname and tests whether or not it exists, and is a regular file.
I want the following url:
http://localhost/new/post?url=sample-post-one
to Look Like this via htaccess mod_rewrite:
http://localhost/new/post/sample-post-one/
This might be a question asked already or a similar one but I have been trying to figure it out since a couple of hours and did not get to a solution.
Any help will be highly appreciated. Thanks!
Update
Here's what I've tried
<Files .htaccess,.svn> order allow,deny deny from all </Files>
Options +FollowSymlinks
# For Removing .php extension from files
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# Rewrite rule
RewriteRule ^post/([a-zA-Z0-9_-]+)/?$ post?url=$1 [L]
This should work :
RewriteEngine on
RewriteRule ^post/([^/.]+)/?$ post?url=$1 [L]
Or better ...
RewriteEngine on
RewriteRule ^post/([a-zA-Z0-9_-]+)/?$ post?url=$1 [L]
By the way, you should have a look to guides like this one.
You can use below code:
http://localhost/new/post?url=sample-post-one
RewriteRule ^post/([0-9]+)/?$ post?url=$1 [NC,L] # Handle post requests
and to get more information you can refer to this
Basically we're capturing the bit for the last folder. The rest can be hard coded. Also you should delete the competing rule. ^ indicates beginning of string. $ is the end of the string. If you use both of those characters everything has to literally match except for the bits where we're using REGEX patterns for matching. Also everything is case sensitive.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^new/post/([^/]+)/?$ new/post?url=$1
If you know that you're only going to allow letters, numbers, periods, and hyphens, then you can do this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^new/post/([^A-Za-z0-9.-]+)/?$ new/post?url=$1
If for some reason you're including new/post and post is really your document root it should look like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^A-Za-z0-9.-]+)/?$ ?url=$1
Apache is a well documented application. They provide an excellent resource with hundreds of examples on their site with explanations of each in the mod_rewrite documentation.
I have an .htaccess file that rewrites urls for SEO purposes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /display.php?page=$1 [L,NC,QSA]
RewriteRule ^search/([^/\.]+)/?$ search.php?what=$1&where=$2 [L,NC,QSA]
Now the first rewrite rule works fine. (www.domain.com/user goes to display.php?page=user)
but the second one should work like (www.domain.com/search/something/else must go to search.php?what=something&where=else
What am I doing wrong here?
Your second rule is incorrect for what you are looking for. You are requesting the result of two captures, but are only making one capture.
Try this instead:
RewriteRule ^search/([^/\.]+)/([^/\.]+)/?$ search.php?what=$1&where=$2 [L,NC,QSA]
Edit: You'll also need to switch your rules around. Your first rule captures everything, and would therefore discard the second.
So, swap them around, and use the L flag, as suggested already.
You are using the [L] flag, which causes mod_rewrite to stop processing the rule set.
Try removing the L flag from the first Rewrite Rule, like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /display.php?page=$1 [NC,QSA]
RewriteRule ^search/([^/\.]+)/?$ search.php?what=$1&where=$2 [L,NC,QSA]
I've been struggling with my .htaccess file for weeks now, I changed it many times but it just won't work.
I have this in my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^/([^./]+)\.html$ category.php?id=$1
RewriteRule ^/([^./]+)\.html$ tag.php?id=$1
RewriteRule ^/([^./]+)\.html$ play.php?id=$1
but it does not work.
Are you sure that mod_rewrite is turned on in Apache? Do you have access to httpd.conf? It would be better to do redirects there instead of with a .htaccess file.
Your conditions are only being applied to the first rule. Each set of RewriteCond's only get applied to the immediately following RewriteRule. So the conditions only get applied to RewriteRule ^/([^./]+)\.html$ category.php?id=$1 and the last 2 rules have no conditions at all.
Your conditions is to rewrite something that exists to something else, which will cause a rewrite loop. You probably wanted:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Your 2nd and 3rd rules will never be applied because if someone requests /some-page.html the first rule's regex will match it and rewrite the URI to /category.php?id=some-page, then the next to rules will never match because the first rule already rewrote the URI to category.php.
Your regular expressions match a leading slash, URI's being applied in rewrite rules that are inside an htaccess file has the leading slash stripped out, so you want this instead:
RewriteRule ^([^./]+)\.html$ category.php?id=$1
1, 2 and 4 is easy. 3, not so much. You're going to have to figure out a unique way to represent an html page as a category, tag, or play. You can't have all 3 look identical, there's no way to tell which one you want. Take:
/something.html
Is that supposed to be a category? A tag? or a Play? Who knows, your rewrite rules surely don't. But if you preface each with a keyword, then you can differentiate:
/category/something.html
/tag/something.html
/play/something.html
And your rules would look like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^./]+)\.html$ category.php?id=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tag/([^./]+)\.html$ tag.php?id=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^play/([^./]+)\.html$ play.php?id=$1