The main navigation of my site is coded like this:
<li>'.$value.'</li>'."\n";
But I would like the URL of the links to look like domain.co.nz/pagename, not domain.co.nz/index.php?pageId=pagename
How would you recommend I accomplish this?
Something like this should work:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?pageID=$1
First line turns on mod_rewrite. Second line sets the base URL to / (it's annoying, but you have to set it to the base path you're dealing with). Third and fourth lines make sure the request doesn't exist as a file, or as a directory. And the last line is the actual magic; basically it searches for "anything", captures what it finds in $1, and "rewrites" the URL to index.php?pageID=$1. If you learn to use regexes, you can do much more complicated things as well.
Yes, you can accomplish this with a .htaccess RewriteRule. In your .htaccess file, include:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?pageId=$1
This means:
If the REQUEST_FILENAME is not a valid file, redirect the entire URL to index.php?pageId=the entire URL
You'd then change your navigation to:
echo "<li>'.$value.'</li>'."\n";
Edit: I moved Trivikrtam's edit inline, see above. The RewriteRule should be index.php?pageId=$1 not /index.php?pageId=$1. Thanks #Trivikrtam!
Related
I have a little mod_rewrite problem and really need some help.
I need to have a second .htaccess file in a subfolder for a project on my domain.
The URL to the project is like:
https://example.com/project-name/
Now I have a query parameter for which I want to create a nice URL with mod_rewrite.
The URL including the parameter looks like this:
https://example.com/project-name/index.php?preset=nameofapreset
And I want it to be:
https://example.com/project-name/nameofapreset
The important thing: The mod_rewrite has to work for the current folder only. Without having any info about the current path, cause I need to use the file in other folders, too.
That's what I thought should work (cause I am using a similar rewrite rule in another project):
RewriteEngine On
RewriteRule ^([a-z])/?$ index.php?preset=$1
But it doesn't. And all my trials fail.
Any ideas how I can make it work?
Thanks!
You can use this rule in /project-name/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?preset=$1 [L,QSA]
I'm working on a .htaccess file and have come across some curious behavior with REQUEST_FILENAME that I'd love some clarification about. I have two rules I'm testing out which are like so:
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ /index.php [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/index.php$ /other_page.php [L]
When I try to go to the address site.com/this_file_exists.php I seem to be getting to other_page.php, which means that
REQUEST_FILENAME was a file in the first RewriteCond but not a file
by the time mod_rewrite processed the second RewriteCond.
After changing the second RewriteRule's flags to [L,E=RF:%{REQUEST_FILENAME}], and echoing $_SERVER['REDIRECT_RF'] on other_page.php, I find that the request filename was just /index.php, as opposed to the original filename, which was /full/path/to/this_file_exists.php.
Does mod_rewrite consistently overwrite the REQUEST_FILENAME in this way after matching a RewriteRule? If so, is there a documented way in which it does that?
These lines in mod_rewrite.c
/* Now adjust API's knowledge about r->filename and r->args */
r->filename = newuri;
seem to suggest that the new REQUEST_FILENAME truly is
the rewritten URI.
I try to make pretty URLs for my website like there:
http://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049
My .htaccess file looks like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^profile/(\d+)*$ ./profile.php?id=$1
It works, root/profile/123 opens root/profile.php?id=123, but the folder paths are now broken:
root/profile.php?id=123 references for example root/css/style.css
but root/profile/123 wants to reference root/profile/css/style.css, which doesn't exist.
I want to have it both ways working: root/profile.php?id=123 and root/profile/123
How can i fix the link problem?
You could add an alias for anything that would be accessed via /root/profile/ with Alias:
Alias /root/profile /real/path/root
See http://httpd.apache.org/docs/2.4/mod/mod_alias.html#alias
If you are limited to a .htaccess file you can add another rewrite rule before the one you already have:
RewriteRule ^root/profile/(.*)$ /profile/$1 [L]
RewriteRule ^profile/(\d+)*$ /profile.php?id=$1
As the first rule would redirect any access to files at root/profile to profile, which is in turn evaluated by the second rewriterule, this would lead to an infinite loop. The flag [L], which stands for "last" tells the rewriteengine to stop further processing, if it encounters a path starting with "root/profile".
I think that I am trying to achieve an impossible result.
The scenario is PURL-Mailing and I already got some URL's rewritten to fit the URL, sent to the customer.
The customer enters the site by the following domain: http://domain.com/UserName
The Variable UserName represents the GET-Variable, which equivalent to http://domain.com/index.php?user=UserName
I achieve this with the following rewrite Rules:
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?name=$1 [QSA]
#This works perfect and translates to http://domain.com/UserName
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?name=$1 [L]
#This achieves the goal but does not reflect in the URI I want:
#http://domain.com/UserName
To go further, there are also some Names containing a dot in the Name like A.Jackson that also need to be treated as UserName. As those are only 13 Name I could implement them manually. What I don't know is how I can prevent the part after the dot to be handled as a file extension. Is there a way to write a custom handle in *mod_rewrite* for those?
And if so, can anybody explain to me how?
Thanks in advance and best regards!
ok try below
RewriteCond %{REQUEST_FILENAME} !(img|anyother folders that you want to ignore|anyother folders that you want to ignore|...)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]*[\.]*[a-zA-Z0-9_-]+)[/]*$ test.php?name=$1 [L]
replace 'anyother folders that you want to ignore' with folder name that you want to ignore. Seperate each folders with '|'
You also have to provide full path to the CSS, image or any other links used in your web page when you using URL rewrite functions
Here is your fix
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]*[\.]*[a-zA-Z0-9_-]+)$ index.php?name=$1 [L]
I've done things very similar before but for some reason I'm having a little difficulty with the specific scenario. I want to pass a folder path as a variable and make it look pretty.
I have a working url like:
http://mysite.com/albums/index.php?p=folder/subfolder/
I can view it without the 'index.php' like:
http://mysite.com/albums/?p=folder/subfolder/
What I want is a pretty url that looks like this:
http://mysite.com/albums/folder/subfolder/
Basically, anything after /albums/ should be a single variable. I've played with my .htaccess RewriteRule a bunch and can't seem to get it working. (get 404 errors) This is what I currently have:
RewriteEngine On
RewriteRule ^albums/(.*)$ albums/?p=$1
below is what i use though every call is directed to my index.php file and from there i do anything with it
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
Hope it helps
Change (.*) into (.+) to ensure there is at least one character after / , otherwise your rule loops...