On my website I am trying to rewrite a long URL to a SEO friendly one.
I've got the following code, but it doesnt seem to affect anything! However if I type dgadgdfsg into my htaccess, it throws an internal server error. So I am presuming it is something with Rewrite Rule.
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /missing-people/user-profile.php?userID=$1&firstName=$2&lastName=$3 [L]
I have confirmed that mod_rewrite is on.
This is the current URL
http://mysite.com/missing-people/user-profile.php?userID=1&firstName=Liam&lastName=Gallagher
and this is what I want it too appear like
http://mysite.com/1/Liam/Gallagher
Change your RewriteRule to this (slightly modified from your version)
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ missing-people/user-profile.php?userID=$1&firstName=$2&lastName=$3 [QSA,L]
If that doesn't work try putting a R flag for testing purpose (which will make your browser change the original URI to: /missing-people/user-profile.php?userID=1&firstName=Liam&lastName=Gallagher
Presuming your userID is comprised only of digits and firstName and lastName are only alphanumeric.
RewriteEngine On
RewriteRule /(\d+)/(\w+)/(\w+)/ /missing-people/user-profile.php?userID=$1&firstName=$2&lastName=$3 [L]
A more strict version that does the same thing except it sets boundaries for the beginning and the end of the evaluated regex.
RewriteEngine On
RewriteRule /^(\d+)\/(\w+)\/(\w+)$/ /missing-people/user-profile.php?userID=$1&firstName=$2&lastName=$3 [L]
Related
I got the follow htaccess:
RewriteEngine On
RewriteRule ^([^-]*)-sub-([^-]*)\.html$ /kat-vergleich/?kat=$1&subkat=$2 [L]
RewriteRule ^([^/]*)\.html$ /kat-vergleich/?kat=$1 [L]
The second one for only kat works fine so
example.com/kat-vergleich/bla-1
is overwritten to
example.com/kat-vergleich/?kat=bla-1
but the first one where
example.com/kat-vergleich/bla-1-sub-morebla
should be rewritten to
example.com/kat-vergleich/?kat=bla-1&subkat=morebla
but it rewrites it to
example.com/kat-vergleich/?kat=bla-1-sub-morebla
And I also can't get the non overwritten parameters like
example.com/kat-vergleich/bla-1?donttouchit=yey
I can't get the yey
echo $_GET['donttouchit'] returns nothing.
Help :/
Your example partly works and partly I think your examples are wrong. Because you match on .html in your second rule and say that it works, but it can not work.
I see you changed your post in the mean time as well. Which makes it a bit more confusing.
But anyway, this seems to work for me if you add the .html:
RewriteEngine On
RewriteRule ^([^-]*)-sub-([^-]*)\.html$ /kat-vergleich/?kat=$1&subkat=$2 [L]
RewriteRule ^([^/]*)\.html$ /kat-vergleich/?kat=$1&%{QUERY_STRING} [L]
Check to make sure that your dashes -> - are the same as in your text and your URL.
my URLs like this
http://example.com/mypage.php
http://example.com/{additional_parameter}/mypage.php
Here, my mypage.php gets {additional_parameter} and works further. My mypage.php work without {additional_parameter} also.
How to code this in htaccess?
Example: http://example.com/user_id/mypage.php will be rewrite to http://example.com/mypage.php?id=user_id
You can give this a try, I have tested it and its working fine.
Note: Here I am assuming your user_id can contain digits, alphabets or _
RewriteEngine on
RewriteCond %{REQUEST_URI} ^\/([\w]+)\/mypage\.php$
RewriteRule .* /mypage.php?id=%1 [L,END,QSA]
OR
RewriteEngine on
RewriteRule ^([\w\s]+)\/mypage\.php$ /mypage.php?id=$1 [L,END,QSA]
Update:
As OP's request url to work on either 0,1,2 or 3 parameters where he can handle its parameters sequence by his own.
RewriteRule ^(([^\/]*)\/)?(([^\/]*)\/)?(([^\/]*)\/)mypage\.php$ /mypage.php?id=$2&class_id=$4&subject_id=$6 [L,END,QSA]
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'm having issues with apaches mod_rewrite. I'm wanting to make clean urls with my php application but it doesn't seem to give the results i'm expecting.
I'm using this code in my .htaccess file:
RewriteEngine on
RewriteRule ^project/([0-9]{4})/([0-9]{2})$ /project/index.php?q=$1&r=$2 [L]
RewriteRule ^project/([0-9]{4})$ /project/index.php?q=$1 [L]
To make it so when I view, http://localhost/user/project/system, it would be the equivelant of viewing http://localhost/user/project/index.php?q=system
Instead of getting any results I just get a typical 404 error.
I've also just checked to see if mod_rewrite works by replace my .htaccess code with this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule (.*) http://www.stackoverflow.com
And it properly redirects me here, so mod_rewrite is definitely working.
The root path to my project is /home/user/public_html/project
The the url used to view my project is http://localhost/user/project
If anymore information is required let me know.
Thanks
If your .htaccess file is indeed located in the project/ subdirectory already, then don't mention it in the RewriteRule again. Remove it:
RewriteRule ^([0-9]{4})/([0-9]{2})$ /project/index.php?q=$1&r=$2 [L]
# no "project/" here
Rules always pertain to the current local filename mapping.
Else experiment with a RewriteBase.
You have [0-9]{4} in your regex which will only match numbers of 4 digits. "system", however, is not a number of 4 digits, and therefore does not match.
You can use something like [^/]+ instead.
RewriteRule ([^/]+)/([0-9]{2})$ /index.php?q=$1&r=$2 [L]
RewriteRule ([^/]+)$ /index.php?q=$1 [L]
Don't know if the second parameter should be a number with 2 digits or not.
Edit: I also added "user" at the beginning now.
Edit2: Okay, I thought you were in the root htdocs with your htaccess. So remove "project" and "user" if you are in "project" with the .htaccess.
You probably mean
RewriteRule ^/project/([0-9]{4})/([0-9]{2})$ /project/index.php?q=$1&r=$2 [L]
RewriteRule ^/project/([0-9]{4})$ /project/index.php?q=$1 [L]
The '^project' means "start of line is 'project'" but the start is a '/project', so you need to include the starting slash (i.e. '^/project...').
Sorry, missed the system bit (and the user bit). Was concentrating on the slash.
RewriteRule ^/user/project/([a-zA-Z0-9]*)/([a-zA-Z0-9]*)$ /user/project/index.php?q=$1&r=$2 [L]
RewriteRule ^/user/project/([a-zA-Z0-9]*)$ /user/project/index.php?q=$1 [L]
Should have you right.
I am trying to learn url rewriting.
My code:
RewriteEngine on
RewriteRule ^/$ /index.php
RewriteRule ^/([a-z]+)$ /index.php?page=$1
When I try it like this: localhost/mysite it shows home page. But when I try something like this: localhost/mysite/abcdefg, it would show a 404 error.
EDIT
What I want to do is:
If only original domain is given, it should goto home page. Eg: www.mysite.com --> www.mysite.com/index.php. Otherwise, if www.mysite.com/contactus --> www.mysite.com/index.php?page=contactus
EDIT
I am using WAMP server in Windows XP.
That's because the first rule would catch the second request. Now, that I took a closer look at the regex, no it would not catch the request. However, your second request would fail. Also, as a rule of thumb the more specialized a rewrite is the higher it should be placed.
You don't need to rewrite all the requests to index, but if you know what you are doing, then re-order the rewrites.
RewriteEngine on
RewriteRule ^/([^/]+)/$ index.php?page=$1 [L]
RewriteRule ^/$ index.php [L]
Edit 1: Taking into account that you are working on a localhost, this would work for you.
RewriteEngine on
RewriteBase /mysite/
RewriteRule ^([^/]+)/?$ mysite/index.php?page=$1 [L]
RewriteRule ^$ mysite/index.php [L]
When you go live, just remove the mysite/ part.
Note: You don't need this rule RewriteRule ^$ /index.php [L] the server will automatically load index.php if you visit localhost/mysite. That is the expected behavior if your server is configured to load a default page, the file index.php, on httpd.conf configuration file.
Edit 2: I see your edit, but you can't test that rewrite in the current URL structure you have in the localhost. You should try and setup virtual hosts to test in an environment that resembles your production as much as possible. Search on Google for how to create virtual hosts for your WAMP, XAMPP, or any other stack you are using.
Then the rewrite rules are simple
RewriteEngine on
# page-url -> index.php?page=page-url
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L]
/localhost/mysite/abcdefg will not match the rule you expect it to match because the path (/mysite/abcdefg) contains a / in the middle that is not matched by your regular expression. So the web server looks for the file, can't find it, and returns a 404.
RewriteRule ^/([a-z]+)$ /index.php?page=$1
will match any string beginning with/ followed by any number of characters a-z. / is not in that range, that is why it fails on /mysite/abcdefg.
#trott and #anders_lindahl are right:
Your first only matches localhost/ aka the root of the site.
The second rule will match anything that has lowercase letters (and just that!) after the first slash, so localhost/thisisavalidstring.
You have put a / in there, so it will not match. use something like:
RewriteRule ^/([a-z\/]+)$ /index.php?page=$1
(haven't tried it, but I assume it will work. I'm not too sure about the need to escape inside the [])
You probably meant to use
RewriteRule ^/([a-z/]+)$ /index.php?page=$1
or
RewriteRule ^/([a-z/]*/)?([a-z]+)$ /index.php?page=$2