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 /.
Related
I need to change the following url
http://somedomain.com/news/a_sample_news_article.html
to
http://somedomain.com/post/a-sample-news-article
I have this in my htaccess which works, but I am sure it can be improved upon - does anyone have a better solution?
RewriteEngine on
# replace underscores
RewriteRule ^(news)/([^_]*)_+(.*)$ /$1/$2-$3 [L,NC,R=302]
# redirect the directory from news to post
RewriteRule ^news/(.*)$ /post/$1 [R,L]
# remove .html from end of url
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
Any help much appreciated!
To redirect
/news/foo_bar
to
/post/foo-bar
you can use the following rule :
RewriteEngine on
# redirect "/news/foo_bar" to "/foo_bar"
RewriteRule ^news/(.+)$ /$1 [L,R]
#2 replace underscore with hypens
RewriteRule (.*)_(.*) $1-$2 [N,E=uscores:yes]
RewriteCond %{ENV:uscores} yes
RewriteRule ^(.+)$ /post/$1 [L,R]
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]
I have an URL like these:
http://domain.com/database/movie/jurassic-park.1224.html
http://domain.com/artist/bruno-mars.104.html
I tried to resolve these URLs with the following RewriteRules, but they don't work.
RewriteEngine On
RewriteRule ^(.*)\/movie\/(.*)\.(.*)\.html$ ./index.php?area=movies&id=$2 [QSA]
RewriteRule ^(.*)\/artist\/(.*)\.(.*)\.html$ ./index.php?area=people&id=$2 [QSA]
Who can help me what's wrong?
Try these rules:
RewriteEngine On
RewriteRule movie/(.+?)\.(\d+)\.html$ index.php?area=movies&id=$2 [L,QSA,NC]
RewriteRule artist/(.+?)\.(\d+)\.html$ index.php?area=people&id=$2 [L,QSA,NC]
I have a few links that look like this, that unfortunately are being used:
http://www.example.com/page/?
http://www.example.com/another-page/??
Is it possible to have them redirect without the question mark(s), or strip them out?
I've tried:
RewriteRule /page/? http://example.com/page/ [R=301,L]
RewriteRule /page/\? http://example.com/page/ [R=301,L]
RewriteRule ^/page/\? http://example.com/page/ [R=301,L]
RewriteRule ^/page/\?$ http://example.com/page/ [R=301,L]
Redirect 301 /page/? http://example.com/page/
You can't match against the query string (or even the ?) in a rewrite rule. Since the query string itself is empty, you can't even match against the variable, %{QUERY_STRING}. What you need to do is match against the actual request:
RewriteCond %{THE_REQUEST} \ /+([^\ \?]+)\?(\ |$)
RewriteRule ^(.*)$ /$1? [L,R=301]
It's going to look real weird with a ? at the end of the rule's target, but that's how you tell mod_rewrite to remove the query string.
I am trying to get this
http://my.url.com/login/ or
to redirect to:
http://my.url.com/app/index.cfm?event=user.login
I am using this rewrite rule and it isn't working.
RewriteCond %{REQUEST_URI} ^/login/$
RewriteRule ^/app/index.cfm?event=user.login [NC,L]
What am I doing wrong?
Thanks
The first argument of a RewriteRule is the pattern which matches against the URI, and you're missing that pattern. Try:
RewriteRule ^/?login/$ /app/index.cfm?event=user.login [NC,L]
If you want to externally redirect the browser, include a R or R=301 (permanent) flag in the square brackets, e.g. [NC,L,R=301].