I am trying to have it so foo.com/userinfo.php?user="username" will be re-written as foo.com/username. I have gotten this to work using RewriteRule ^([a-zA-Z0-9_-]+)$ userinfo.php?user=$1 in my htaccess. However, when a slash is added to the end of the username such as foo.com/username/ it just redirects it to foo.com/userinfo.php?user="username". While this does pull up the profile it doesn't look as well in the url bar. Thank you for any help!
Here is my .htaccess code now
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ userinfo.php?user=$1
You can use `/?' as the expression "the character / or nothing".
RewriteRule ^([a-zA-Z0-9_-]+)/?$ userinfo.php?user=$1
Related
A normal link of my site may look like this
mydomain.com/categorylist/8/Special_Single_Songs.html
I'm planning to change the URL pattern to something like
mydomain.com/categorylist/8-Special-Single-Songs.html
How can I redirect the old URL pattern to the new one using .htaccess redirect rule?
My .htaccess Rewrite rule look like this
RewriteRule ^categorylist/([0-9]+)/([0-9a-z]+)/([0-9]+)/(.*)\.html$ /index.php?pid=$1&sort=$2&page=$3 [L]
Try this code
RewriteRule ^categorylist/([0-9]+)/([0-9a-z]+)/([0-9]+)/(.*).html$ /index.php?pid=$1&sort=$2&page=$3 [L]
RewriteRule ^categorylist/([0-9]+)\-([0-9a-z]+)/([0-9]+)/(.*).html$ /index.php?pid=$1&sort=$2&page=$3 [L]
try this & put it on top after RewriteEngine on
RewriteRule ^categorylist([^_]*)_([^_]*_.*).html$ $1-$2 [L,NE]
RewriteRule ^categorylist([^_]*)_([^_]*).html$ /$1-$2 [L,NE,R=301]
note : big number of underscores can be a problem
I have a problem in creating SEO friendly url actually I have two url rewritten one is working and other one is not. I dont know why? please help
My url is this
http://localhost/quotesnew/author.php?authID=1
and I want it to be
http://localhost/author.html
here is my code in .htaccess file
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /quotesnew/index.php?authchar=$1 [L]
RewriteRule ^author\.html$ /quotesnew/author.php?authID=1 [L]
You can try
RewriteEngine On
RewriteRule ^author/([^/]*)\.html$ /quotesnew/author.php?authID=$1 [L]
Now your url would be
http://localhost/author/1.html
The [L] flag stops rewriting for this cycle. It therefore never reaches the second rule.
I try to rewrite my SEO URLs to some real GET requests, to handle in my PHP file.
I want to have these 2 cases to work:
mysite.com/company-profile -> index.php?action=company-profile
mysite.com/faq/howcanijoin -> index.php?action=faq&anchor=howcanijoin
I got the first case to work using the rule:
RewriteRule ^([A-Za-z0-9-]+)$ index.php?action=$1
For the second I tried also. I put this rule before the previous one:
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?action=$1&anchor=$2
But it's not working. Any suggestions? If I understand correctly inside each parenthesis goes variables $1, $2 etc?
Do this
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/(.*)$ /index.php?action=$1&anchor=$2
This?
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?action=$1&anchor=$2 [QSA,L]
the incoming URL
domain.com/12/3
should be re-written to
domain.com/?w=12&h=3
htaccess
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?w=$1&h=$2 [QSA]
the php
<?php
echo $_GET['h'];
?>
Result
404 page
I've tried using htaccess to change the result of the url and then retrieve the value from the URL, could someone help me out?
Maybe you need to escape - like:
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_\-]+)$ index.php?w=$1&h=$2 [QSA]
PS. I hope the above URL /12/3 is just for example because your regex accepts only a-z
RewriteEngine On
RewriteRule ^[a-z]{2,2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Essentially, I'm pretty sure you need a / in front of index.php
I like to use this generator for my mod-rewrite rules.
http://www.generateit.net/mod-rewrite/
Try changing your rewriterule to the following:
RewriteEngine on
RewriteRule ^([a-z0-9]{2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Your example has two digits as the first parts of the path, which won't be matched by [a-z].
RewriteEngine On
RewriteRule ^(.*)/(.*)$ index.php?w=$1&h=$2 [QSA]
The above sorted it. Thanks for the replies
I have some code which uses Rewrite engine in my .htaccess file and it specifies the directory for my SERPs. However when someone goes onto search/QUERY/ rather than search/QUERY/PAGE/ it displays a 404 error. Same with just search/.
I want it so that if someone just goes to search/QUERY/ that it redirects them to search/QUERY/1/ and for just search/ it redirects them to my homepage /. I have included a copy of my code below.
RewriteEngine on
RewriteRule ^search/([^/]+)/([^/]+)/?$ search.php?q=$1&category=web&d=$2
Can anyone help me with this problem?
Thanks in advance, Callum
Try this code in your htaccess :
RewriteEngine on
RewriteRule ^search/?$ / [R=301,L]
RewriteRule ^search/([^/]+)/?$ /search/$1/1/ [R=301,L]
RewriteRule ^search/([^/]+)/([0-9]+)/?$ search.php?q=$1&category=web&d=$2 [NC,L]
You can create multiple RewriteRule statements to accomplish this. (Make sure to omit the [R] flag on intermediate rules if you use it!)
The rule you posted will only rewrite if it's of the form /search/query/n/, so write a regular expression matching search/query that redirects to /search/query/1. Do the same to redirect home with no query.
Try these additional 2 rules in your .htaccess file:
RewriteRule ^search/([^/]+)/$ /search/$1/1/ [R=301,L]
RewriteRule ^search/?$ / [R=301,L]