I have pretty much developed an entire site and was foolish to not implement mod_rewrite. I wish to do so now, and did a test page. The mod_Rewrite works perfectly, but I then have the issue of all of the imaegs being linked to the new pages URL, for example:
www.mysite.com/county/Kent.html
from
www.mysite.com/sector.php?county=kent
It works great, but then when I am on the new link the URL for images changes to this.
mysite.com/county/images/widMot.png
As if the 'images' folder is in the 'county' folder which actually doesn;t exist. Is there a rewrite rule, which will ignore this image link change? Or something I am missing?
EDIT: As requested:
...
RewriteEngine On
RewriteRule ^county/([^/]*)\.html$ /sector.php?county=$1 [L]
...
EDIT: The solution the answer posted was excellent, and resolved the image issue, but I was then stuck with an issue of my css files being put after /images/ directory. Ofc a simple fix would be place them in there, but is there no other way?
Please take a look at the <base> HTML tag.
http://www.w3schools.com/tags/tag_base.asp
It allows you to define the base URL of your site, after that, any link built like /images/someimage.png will direct you to the root directory's images/.
EDIT:
To be more specific, all relative URLs starting with a forward slash / will begin immediately after domain name.
Try something like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule YOUR REWRITE RULE
</IfModule>
Should pass everything to your rewrite rule, unless the requested URL is an actual file on the server.
Related
I have recently change my hosting and i need a htaccess rewrite rule for my files. I tried many examples but no one really works for my case. I have never been really good in htaccess and on my older hosting i didn't really need anything it just worked but here is not. Basically i want that my PHP files are without extensions and treated like a directory. So for example i have a URLs like these:
www.domain.com/file1/{id}/{nick}
So for example:
www.domain.com/myfile1/104/username
www.domain.com/myotherfile/455/nick
File1 in this case is a PHP file and {id} and {nick} are changable. I have this structure on my entire site for many other PHP files etc. So if possible i want one universal rule for all files. I tried with htaccess to remove php extenstion etc but all I got is 404 error. Also URL in browser should stay friendly without PHP extension. So in my case if i rewrite my URL manually in:
www.domain.com/file1.php/{id}/{nick} it worked but i don't want to change all the links etc on my website. So all i want is to hide PHP extension and treat PHP files as directory.
Thanks
You can use this single and generic front controller rule in site root .htaccess:
AcceptPathInfo On
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/(.*)$ $1.php/$2 [L]
I would like to have clean URLs in my projects. So I've written these codes in a .htaccess file.
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?page=$1&id=$2 [NC,QSA,L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [NC,QSA,L]
But it does not work completely when I'm trying to work with it locally.
Imagine that I have a directory myproject in htdocs (www) in my local web server path and other files are stored in this folder. Now I can see the project if I go to localhost/project.
Now I want to work with URLs.
It works well if I have only one parameter in URI like localhost/myproject/tours. But if I have 2 parameters like localhost/myprojects/tours/inside, it seems that all css, js and images files go away. I've also added RewriteBase /myproject to .htaccess file, but nothing solved.
What is my mistake? I need a solution that works on both remote and local server.
First of all, see my response on your other question about your code: Why .htaccess mod_rewrite does not show a real directory
Now, RewriteBase won't solve your problem about css/js/images etc. It's only for htaccess and it defines the base path when a rule is rewritten.
One common way to avoid this problem is to add in all your files a base url right after <head> html tag.
For you, it would be: <base href="http://localhost/myproject/" />
Otherwise, if you reach localhost/myprojects/tours/inside then your css/js/images links will be resolved as localhost/myprojects/tours/inside/__here__ because the default base path here is the current directory (/myproject/tours/inside/) and this is not what you want
Edit: if that's the case, don't forget to remove leading slashes from your css/images/javascript html links
The browser will build absolute URL paths out of your relative URL paths by looking at your made up context of /myprojects/tours. You may need to strip one or two levels of that prefix off to find the real path.
The access log will show you plain as day what relative URL's come in when you use the old and new URLs.
I have been trawling the tutorials on htaccess but every example seems to have an absolute URL in the second part of the rewrite. My site delivers across several domains so I need a x-domain solution.
Rule should state the following: Everything that did go to the confirm.php file now goes to confirm/ folder. So...
http://example.com/confirm.php?token=32879 should now go to http://example.com/confirm/?token=32879
AND
http://elpmaxe.com/confirm.php?token=32879 should now go to http://elpmaxe.com/confirm/?token=32879
The code I have come up with is:
RewriteCond %{REQUEST_URI} confirm.php$ [NC]
RewriteRule ^(confirm\.php)$ confirm/ [L,R=301]
But this appends the entire document root path to the end of the URL as follows:
http://example.local/var/www/vhosts/example.co.uk/confirm/?t=CB3Qj&e=578
Please help!
This rule should work:
RewriteEngine On
RewriteRule ^(confirm\.php)$ /confirm/ [L,R=301,NC]
Make sure to test in a new browser to avoid 301 caching issues.
So I'm using a simple rewrite rule like:
RewriteEngine On
RewriteRule ^foo/?$ foo.php [NC,L]
It redirects perfectly to foo.php, but it seems like all links and images in foo.php are being taken from folder foo on the server which doesn't exist. For instance, 1.png will now be foo/1.png or index.html will now be foo/index.html. So my question is: is there any way to make thing right without changing paths to the files in foo.php?
Your visitors' browsers see the current page as being at /foo/, thus all relative URLs will be resolved under /foo/. You will need to set the base URL, or update all your relative URLs to point to your site root (e.g. do not use relative/path/url.jpg but /relative/path/url.jpg).
In your code you should provide a rewrite rule for your resources (images, css, etc...) or add conditions for real files / folders like...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
The two RewriteCond lines test to see if the requested URL points to an actual real directory (the !-d part), and the second one tests if it's a real file (!-f)
In the future, you can easily debug your mod_rewrite stuff by adding this two lines to your .htaccess file:
RewriteLogLevel 3
RewriteLog "/path/to/a/file.log"
2 simple ways
absolutize the references as suggest by Ianzz
remove the foo path for referenced object still using htaccess
RewriteRule ^foo(/.*.(jpg|html|gif|css))$ $1 [L]
I prefer the 2nd solution because the htaccess do the mess and htaccess fix the situation, and no changes to your code are needed.
I just redeveloped an existing site from the ground up. The old site used pure HTML while the new site will draw content from a database. Now I need to redirect the old pages to the new URL structure, but I’m a bit confused about how to write the .htaccess rules for a file-to-file redirect.
In all the examples I’ve found, the first part of the rule looks like an absolute directory path from the root, but they only contain the part of the URL that immediately follows the domain name.
For instance, I want to redirect
https://garrettcounty.us/archives/12262011news.html
to
https://garrettcounty.us/news/20111226/house-fire-on-christmas-day
From the examples I’ve seen (both on StackOverflow and abroad), I guess the rule would be
redirect 301 /archives/12262011news.html https://garrettcounty.us/news/20111226/house-fire-on-christmas-day
but the actual path to the original file on the server is
/home/username/public_html/archives/12262011news.html
Should I use the directory path or the path from the domain?
I would love to be able to use a rewrite rule. Unfortunately, the original developer didn’t use a consistent file naming scheme so I’m faced with things like
12262011news.html
Jan-19-2012-Headlines.html
State-Of-The-Union-25jan2012.html
In the new model, I'm directing everything through index.php with
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
so if anyone knows of an easier way to map all the old pages to the new URLs, I’d love to hear it. As it stands, it looks like I have to redirect 70+ pages one-by-one.
Well you old URL doesn't have news title so obviously mod_rewrite cannot create it. However to redirect
https://garrettcounty.us/archives/12262011news.html
to
https://garrettcounty.us/news/20111226/
you can use code like this in your .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} =on
RewriteRule ^archives/(\d+)([^.]*)\.html$ https://garrettcounty.us/$2/$1/ [R=301,L,NC]
Path used in Redirect directives: For mod_alias or mod_rewrite you must use path relative to DOCUMENT_ROOT not the full path on filesystem.