Rewrite rule is not working properly - .htaccess

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]

Related

htaccess, no variable go to allcategories.php

I have the htaccess rule above:
RewriteRule ^category/([\w-]+)/(\d+)/?$ category.php?categoria=$1&id=$2 [L,QSA,NC]
RewriteRule ^category/([\w-]+)/?$ category.php?categoria=$1&id=1 [L,QSA,NC]
RewriteRule ^category/ allcategories.php
So if I have category/some_name or category/some_name/id it will redirect to category.php.
If I have only category/to allcategories.php.
The problem is urls like this:
category/j.j._name
category/victoria%27s_secret
It has a value (j.j...) and it is redirecting to allcategories.php instead of category.php. What is wrong? is it the special characters? how to solve?
You need to tweak your regex to allow special characters:
RewriteRule ^category/([^/]+)/(\d+)/?$ category.php?categoria=$1&id=$2 [L,QSA,NC]
RewriteRule ^category/([^/]+)/?$ category.php?categoria=$1&id=1 [L,QSA,NC]
RewriteRule ^category/?$ allcategories.php [L,NC]
[^/] will match anything except a /.

Im trying to combine three various clean url rules

my problem is that I want to get three simple rules working, but my knowledge is too little, to get them working together:
These are obvious:
RewriteRule ^login$ /login.php [L]
RewriteRule ^register$ /register.php [L]
domain.com/login and domain.com/register
Secondly, since i have only one page used for displaying data, i want its url to be as simple as posible, like:
RewriteRule ^(.*)$ /data.php?id=$1 [L]
which should be translated into:
domain.com/1a2s3d
As third, I want to be able to change url with activation code:
RewriteRule ^register/activate/([^/]+)$ /register.php?action=activate&code=$1 [L]
which finally should be translated into:
domain.com/register/activate/some-hash
I know just simply basics. And I cannot mix all of these three ideas into one single working htaccess file. With the second rule the server gives me 500 error, with third rule registration page works, but css file path is translated into domain.com/register/activate/theme/style.css instead of domain.com/theme/style.css
Any help would be appreciated.
Try just with that:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^login$ /login.php [L]
RewriteRule ^register$ /register.php [L]
RewriteRule ^register/activate/([^/]+)$ /register.php?action=activate&code=$1 [L]
RewriteRule ^(.*)$ /data.php?id=$1 [L]

How do I allow numbers in htaccess rewrite rule?

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]

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 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