How do I allow numbers in htaccess rewrite rule? - .htaccess

I have a htaccess setup like so:
RewriteRule ^([A-Za-z_]+)$ profile.php?name=$1 [NC,B,QSA]
it works fine however it doesn't work when the parameter has a number in it.
also is it possible to allow periods and commas?
What can I change?

Change the regex:
RewriteRule ^([0-9A-Za-z_]+)$ profile.php?name=$1 [L,QSA]
OR even better
RewriteRule ^(\w+)/?$ profile.php?name=$1 [QSA,L]
Since \w is same as [0-9A-Za-z_]
OP's comment:
is there a way to let it use periods and commas as well?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w,.]+)/?$ profile.php?name=$1 [QSA,L]

Related

Rewrite rule is not working properly

I am trying to write SEO friendly usls using Apache Mod-Rewrite.
My URLs are something like this.
index.php?p=edit-profile
index.php?p=edit-profile&id=3
index.php?p=edit-profile&id=3&city=sydney
index.php?p=edit-profile&id=3&city=sydney&name=some example text
This is how I tried it.
RewriteRule ^([\w-]+)/?$ index.php?p=$1 [L,QSA,NC]
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?p=$1&id=$2 [L,QSA,NC]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?p=$1&id=$2&city=$3 [L,QSA,NC]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?p=$1&id=$2&city=$3&name=$4 [L,QSA,NC]
Here, first, second and third rules are working for me. But my problem is If I have a string with spaces for fourth variable my forth rule is not working. And also if I have a single word for forth one again it doesn't work.
Can anybody tell me how I fix this problem. Is there a way to replace these spaces with hyphens?
Thank you.
You can include space in your character class:
RewriteRule ^([\w-]+)/?$ index.php?p=$1 [L,QSA,NC]
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?p=$1&id=$2 [L,QSA,NC]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?p=$1&id=$2&city=$3 [L,QSA,NC]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\s\w-]+)/?$ index.php?p=$1&id=$2&city=$3&name=$4 [L,QSA,NC]

Two rewrite rules don't cooperate

I'd like to rewrite two things on one site.
mysite.com/something -> mystie.com/index.php?s=something
and
mysite.com/something/another -> mysite.com/index.php?s=something&d=another
This is my htaccess
RewriteEngine on
RewriteRule ^(.+)$ index.php?s=$1 [L,QSA]
RewriteRule ^(.+)/(.+)$ index.php?s=$1&d=$2 [L,QSA]
Separately both work but together they don't...
I'm guessing the problem is that you're matching the '/' character in your first rule. Wouldn't the easiest solution be to simply add a character class to your rules, like:
RewriteRule ^([a-z0-9]+)$ index.php?s=$1 [L,QSA]
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)$ index.php?s=$1&d=$2 [L,QSA]
or simply change the order of the rules:
RewriteRule ^(.+)/(.+)$ index.php?s=$1&d=$2 [L,QSA]
RewriteRule ^(.+)$ index.php?s=$1 [L,QSA]
Reverse the two rules. The rewrite engine goes through the file line by line, sequentially. The first rule you have:
RewriteRule ^(.+)$ index.php?s=$1 [L,QSA]
matches a url that has two name value pairs as well, so the file stop processing after the first match because you are using the L flag.
RewriteRule ^(.+)/(.+)$ index.php?s=$1&d=$2 [L,QSA]
RewriteRule ^(.+)$ index.php?s=$1 [L,QSA]
I don't have time to test this for you, sorry. But I'm feeling strongly that this is the problem. While I was writing this answer, someone gave pretty much the same advice. I thought of deleting my answer but want to make sure that you understood about the L flag and that the file is processed sequentially.
RewriteEngine on
RewriteRule ^([a-z0-9]+)$ index.php?s=$1 [L,QSA]
RewriteRule ^([A-Za-z0-9]+)/(.+)$ index.php?s=$1&d=$2 [L,QSA]
This works. Changing order didn't work. Also I forgot add that
mysite.com/something/another
the another string can be like asd-1231-ASDwqda .

.htaccess case sensitive and mod_rewrite

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.

htaccess rewrite underscores to dashes for a specific directory

I can successfully rewrite underscores to dashes with the following -- but I need the code to work ONLY for a certain directory, and I can't get that part to work.
WORKS:
RewriteRule ^([^_]*)_([^_]*)_(.*)$ /$1-$2-$3 [R=301,L]
RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L]
DOES NOT WORK:
RewriteRule ^/media/entry/([^_]*)_([^_]*)_(.*)$ /media/entry/$1-$2-$3 [E=unscors:Yes]
RewriteRule ^/media/entry/([^_]*)_(.*)$ /media/entry/$1-$2 [E=unscors:Yes]
RewriteCond %{ENV:unscors} ^Yes$
RewriteRule ^/media/entry/(.*)$ http://domain.com/media/entry/$1 [R=301,L]
When you remove all leading / from RewriteRule patterns all should work fine (as in the first two example which work).
The the following code:
RewriteRule ^media/entry/([^_]*)_([^_]*)_(.*)$ /media/entry/$1-$2-$3 [E=unscors:Yes]
RewriteRule ^media/entry/([^_]*)_(.*)$ /media/entry/$1-$2 [E=unscors:Yes]
RewriteCond %{ENV:unscors} ^Yes$
RewriteRule ^media/entry/(.*)$ http://domain.com/media/entry/$1 [R=301,L]
There is also nice online htaccess tester which will help you out verify your configuration e.g.
The tool has some limitations though i.e. currently does not have implemented certain features like %{REQUEST_FILENAME} or %{ENV:...} but for simple rules it should be fine.
I hope that will help.

htaccess mod rewrite 2 words

I have this code:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ראשי$ index.php?page=main [L,QSA,NC]
RewriteRule ^ניהול גלריות$ index.php?page=galleries [L,QSA,NC]
RewriteRule ^ניהול דפים$ index.php?page=pages [L,QSA,NC]
RewriteRule ^ניהול משתמשים$ index.php?page=users [L,QSA,NC]
RewriteRule ^הגדרות כלליות$ index.php?page=settings [L,QSA,NC]
RewriteRule ^דיווחים$ index.php?page=reports [L,QSA,NC]
</IfModule>
It's making an 500 internal server error, if I only put the lines with one word like "דיווחים" it works but when I add those with two words like "ניהול דפים" not working.
How can you fix it? (maybe the problam is in the space between the 2 words?)
The language isn't the problem even if I use English it's not working.
My address is like /ניהול דפים
What you are writing there essentially are regular expressions. You might want to try using a regular expression meta character to indicate the presence of a space.
Why don't you try something like this -
RewriteRule ^ברזילאי\sדן$ index.php?page=galleries [L,QSA,NC]
The \s indicates whitespace.
mod_rewrite supports logging, so you could could add this to get more details:
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 2
Also check apache's error_log, as it usually writes something on a 500 error.

Resources