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
Related
I am trying to rewrite
http://example.com/category/this-is-my-category
to...
http://example.com/category.php?id=this-is-my-category
My .htaccess file is below:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^category/(\d+)/([\w-]+)$ /category.php?id=$1 [L]
This gives a 404 error
http://example.com/category.php exists on the server
I have also tried
RewriteRule ^category/(\d+)/([\w-]+)$ ./category.php?id=$1 [L]
and
RewriteRule ^/category/([a-zA-Z0-9]+)$ /category.php?id=$1
I have read some articles on this and can't see an issue with the code in the .htaccess file.
Right, so your first regex...
^category/(\d+)/([\w-]+)$
requires a number between category and the last part, eg /category/1234/something-else.
Your second regex...
^/category/([a-zA-Z0-9]+)$
has an incorrect leading slash (rewrite rules start at the rewrite-base) and requires only letters and numbers after category, eg /category/thisIsMyCategory.
The URL you're testing has letters and hyphens.
To me, it looks like you want
RewriteEngine On
RewriteRule ^category/([\w-]+)$ /category.php?id=$1 [L,QSA]
Demo ~ https://htaccess.madewithlove.be?share=8c3de5aa-68f3-5ec0-9c69-23ff2dbe2d6e
Some notes...
It's rare to ever need RewriteBase, especially if your .htaccess file is in the root directory so I've removed it
I've added the QSA flag so any query parameters are preserved. For example
/category/this-is-my-category?foo=bar
becomes
/category.php?id=this-is-my-category&foo=bar
I'm trying to figure out how to rewrite urls using apache webserver and php.
The url below is the real nonrewritten url:
http://localhost:1337/rewritetest/index.php?id=12
And I want to reach it by
http://localhost:1337/rewritetest/index/12
My indexfile looks like this:
<?php
echo $_GET['id'];
?>
Is this possible? The "new" url doesn't include any parameter names so I guess I have to use an order of parameters instead but I dont know how to reach them in that case.
Below is as far I've come with my rewrite:
RewriteCond %{QUERY_STRING} id=([-a-zA-Z0-9_+]+)
RewriteRule ^/?index.php$ %1? [R=301,L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?id=$1 [L]
Anyone have an idea of what I'm doing wrong?
it's located in the same folder as index.php
So, given the .htaccess file is located at /rewritetest/.htaccess (as opposed to the document root ie. /.htaccess) then...
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?id=$1 [L]
If you request a URL of the form /rewritetest/index/12 then the above RewriteRule pattern won't actually match anything. It tries to match "index/12", but your pattern does not contain a slash so will fail. (Is the + inside the character class intentional?)
Try something like the following instead:
RewriteRule ^(index)/(\d+)$ $1.php?id=$2 [L]
This obviously specifically matches "index" in the URL. If you are always rewriting to index.php then you don't really need "index" in the URL - unless this means something different? This also assumes that the valuue of the id parameter consists only of digits.
To rewrite the more general .../<controller>/26 to .../<controller>.php?id=26 (as mentioned comments) then try something like:
RewriteRule ^([\w-]+)/(\d+)$ $1.php?id=$2 [L]
In per-directory .htaccess files the slash prefix is omitted on the URL-path that is matched by the RewriteRule pattern, so /? is not required. The above pattern also matches something for for the id, not anything. So, /index/ would not match.
If this is a new site then the "redirect" (from /index.php?id=12 back to /index/12) is not necessarily required. That's only really required if you are changing the URL structure on an existing site where old URLs already have inbound links and are indexed by search engines. In which case you could do something like the following before the internal rewrite:
RewriteBase /rewritetest/
RewriteRule %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^id=(\d+)
RewriteRule ^(index)\.php$ $1/%1 [R,L]
Or, for a more generic .../<controller>/26 to .../<controller>.php?id=26 (as above) then change the RewriteRule to:
RewriteRule ^([\w-]+)\.php$ $1/%1 [R,L]
The additional check against the REDIRECT_STATUS environment variable is to prevent a rewrite loop after having rewritten the URL to /index.php?id=12 earlier.
I have the following code in my .htaccess.
RewriteEngine On
RewriteRule ^/(\w+)/?$ /?user=$1
I'm trying to rewrite
http://domain.com/?user=username into http://domain.com/username. Unfortunately this code doesn't rewrite anything. Please help
Note:
I checked phpinfo() and mod_rewrite is loaded.
Update
I need to get username from url like http://facebook.com/username. But this code rewrites every folder in root folder, so my /css folder become http://domain.com/css/?u=common. How to allow this code works only for http://domain.com/index.php
The mistake you are doing is the use of / in the beginning of the line ^/(\w+)/?$
rewrite rules strips off the / from the beginning of the pattern to be matched in .htaccess and directory context.
Try doing this:
RewriteEngine On
RewriteRule ^(\w+)/?$ /?user=$1
From RewriteRule Directive docs :
What is matched?
In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html").
In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that lead the server to the current RewriteRule (e.g. "app1/index.html" or "index.html" depending on where the directives are defined).
If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.
Edit: Answer updated as per OP's request:
Add this :
RewriteEngine On
#do nothig if URL is trying to access the folder CSS.
RewriteRule *css/* - [L]
#checks where the URL is a valid file/folder.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/?$ /?user=$1
I think that you are doing it the right way round, but explained it the wrong way round!
Is the problem that you don't need the initial / as the URL passed to test doesn't include it!?
I suspect it should be RewriteRule ^(\w+)/?$ /?u=$1
Also, be careful you don't end up with a loop!
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]
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.