My mod_rewrite code is:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)?$ index.php?url=$1 [L,NC]
This structure is placed on my server path /beta
When I place the URL http://mydomain.com/beta/download everything works rewriting to the real path http://mydomain.com/beta/index.php?url=download
The problem happens with the relative paths. When I enter the URL http://mydomain.com/beta/download/box the rewriting goes to http://mydomain.com/beta/index.php?url=download/box... but....
All my relative paths, like my css folder, my js folder, image files.... everything with relative path in my index.php page crashes, cuz my relative paths become download/css, download/js, download/images... etc...
I know this problem happens in reason of the slash in the path "download/", but how can I solve that using the mod_rewrite in .htaccess?
Can I prevent rewriting in relative paths? Or even in internal paths of my own domain?
How can I solve that WITHOUT change my relative paths to absolute paths?
Thanks
Add a <base href="yoururl.com/beta/"; /> tag to your HTML head. Also add RewriteBase /beta/ to the htaccess if you're in that subfolder
you should always use absolute paths for images css etc, when playing with mod_rewrite but simple solution might be to ignore css,js, images and rest in RewriteRule ^(.+)?$ index.php?url=$1 [L,NC]
RewriteRule ((js|css|images)\/.*)$ /$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)?$ index.php?url=$1 [L,NC]
however this might not work if these dirs actually depend of the current url
I've tested the tag and there's no need to indicate http:// The following code worked for me:
<base href="<?php echo dirname($_SERVER['PHP_SELF']);?>/" />
The server won't ever know whtether the request came via relative or absolute path so there is nothing You can do about it. As stated in another answer, You could make exceptions but maintaining all of them etc will become PITA at some point. ;)
I always use absolute paths especially when rewriting so I'd suggest just that, it's the easiest way
Adding the line:
<BASE href="http://www.sitename.com/">
to my page inside the <head> tag works great for me. It can be coded even a PHP function to automatize this so you don't have to bother with copy/paste on other occasions.
Update:
<?php
$BaseDir = 'http://'.$_SERVER['SERVER_NAME'].substr($_SERVER["SCRIPT_NAME"], 0, strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
?>
<head>
...
<BASE href="<?php echo $BaseDir; ?>">
...
</head>
Related
I'm trying to use pretty URLs on my site, and I don't know too much about htaccess, but after some search I ended up with this:
RewriteEngine On
RewriteRule ^([a-z]+)\/([0-9]+)\/?$ content.php?action=$1&id=$2 [NC]
I'm trying to change this:
mysite.com/cms/content.php?action=content&id=80
into:
mysite.com/cms/content/80
But somehow it's not working and what I get is a blank screen and a 404 error,
Failed to load resource: the server responded with a status of 404 (Not Found) --in-- /cms/files/plugin/tinymce/tinymce.min.js
About the 404 error: It should load a .js file from mystie.com/files/plugin/tinymce/tinymce.min.js -- not -- mystie.com/cms/files/plugin/tinymce/tinymce.min.js
My htaccess file is in the root folder. Am I doing something wrong?
I think the server did not read my .htaccess file. How do I find out the issue?
You need to place this rule in /cms/.htaccess:
RewriteEngine On
RewriteBase /cms/
RewriteRule ^([a-z]+)/([0-9]+)/?$ content.php?action=$1&id=$2 [NC,L,QSA]
Then you need add this in the <head> section of your page's HTML: <base href="/" /> so that every relative URL is resolved from that URL and not the current page's relative URL.
RewriteEngine On
RewriteRule ^/cms/([a-z]+)/([0-9]+)$ content.php?action=$1&id=$2 [NC]
Remove escape slashes \
You need to make sure to include the subfolder if your htaccess is in the root.
Try this rule.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cms/([a-z]+)/([0-9]+)/?$ /cms/content.php?action=$1&id=$2 [NC,L]
Also a blank screen usually means a PHP error.
You have /cms/ in your URL before the rest, so you need to change RewriteRule to something like this:
RewriteRule ^cms/([a-z]+)\/([0-9]+)\/?$ content.php?action=$1&id=$2 [NC]
Also, I hope you are aware that .htaccess can not work on some servers (like nginx or some custom ones), and even on Apache servers there can be mod_rewrite disabled.
My guess is that your scripts and styles are being linked to via relative URLs. Because the old URL is /cms/content.php the URI base is /cms/ and your relative links all resolved to be under /cms/, which I assume is what you want.
However, when you change your links to /cms/content/1234, the URI base becomes /cms/content/ and all relative links will use this as its base, and since "content" isn't even a real folder, all those links will 404, and you won't load any of your CSS content or scripts.
Either change your links to absolute URLs or specify a URI base in the header of your pages (inside the <head> </head> tags):
<base href="/cms/" />
Here is my problem
in my website base url am redirecting to an inside folder
eg: http://example.com/
to
http://onlinevyapari.com/business/ by using this code in .htaccess file
RewriteRule ^(/)?$ /business/index.php [L]
now I want to use search engine friendly url for the same website my query is like this
http://example.com/business-details.php?id=106
but I want to keep my url like this
http://example.com/business-details/106
I have done in different way like bellow
RewriteEngine on
RewriteRule business-details/id/(.*)/ business-details.php?id=$1
RewriteRule business-details/id/(.*) business-details.php?id=$1
its happening but css is not loading properly
it will be really appreciable to me if somebody help.
thank you
Have your rule like this:
RewriteEngine on
RewriteRule ^(/)?$ business/index.php [L]
RewriteRule ^business-details/(\d+)/?$ business-details.php?id=$1 [L,QSA,NC]
For solving css/js/image path issues just use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
Alternatively you can try adding this in your page's HTML header: <base href="/" /> so that every relative URL is resolved from that URL and not the current URL.
You need to add the following, before your rules, to make the rewrite rules ignore file and directory names.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
I have a problem with my URL, due to my htaccess file (i think) for making "pretty URLs".
Everything works like it should when I give a URL with one parameter like this:
localhost/project/page
But when I add another parameter at the end of the URL like this:
localhost/project/page/parameter
the page cannot find images, even though the path is absolutely the same and the second parameter in the URL is irrelevant for the page.
Here is my the content of my htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
if you think the problem is occuring for some other reason, please let me know.
Any help will be greatly appreciated.
This is the most common problem people face when switching to pretty URL schemes.
This problem is happening because you're using relative paths in your css/js/image paths. You have 2 options to fix this problem.
Use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
You can try adding this in your page's HTML header: <base href="/" /> so that every relative URL is resolved from that URL and not the current URL.
I was recently asked if I could make my friends server address more user friendly. His current urls looks like this:
http://wwww.example.com/site/index.php?page=home
http://wwww.example.com/site/index.php?page=about/john
http://wwww.example.com/site/index.php?page=portfolio/concept-art/2013
He would like them to look like this
http://wwww.example.com/site/home
http://wwww.example.com/site/about/john
http://wwww.example.com/site/portfolio/concept-art/2013
which I thought would be pretty simple so I wrote this following rewrite.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|ico|include|js)/
RewriteRule ^([^_]*)$ index.php?page=$1 [L]
which seems to work for the basic links like
http://wwww.example.com/site/home
but for something like this
http://wwww.example.com/site/about/john
none of the css or js will load. So I fixed that for now by making all of the files absolute paths but I am worried that my friend is going to add a new plugin or something and forget that he has to make it an absolute path.
My Question
Is there something I could change or add in my htaccess file to get the css and js files to load with a relative path? If so what would I need to do?
Better to use this rule:
RewriteEngine On
RewriteBase /site/
RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|ico|include|js)/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?page=$1 [L,QSA]
Then for css/js/images better to use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
Alternatively You can try adding this in your page's header:
<base href="/" />
OR
<base href="http://domain.com/site/" />
Is there something I could change or add in my htaccess file to get the css and js files to load with a relative path? If so what would I need to do?
You could just add the proper relative URI base in your page header:
<base href="/site/" />
Or you could brute force redirect them using mod_rewrite (not preferable):
RewriteRule ^(.+)/(admin|css|fonts|ico|include|js)/(.*)$ $2/$3 [L]
Hi I am using the following lines in my .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteBase /coaster/CoasterInsider/
RewriteRule signup$ index.php?page=signup [L,NC]
RewriteRule login$ index.php?page=loginHandle [L,NC]
RewriteRule u/(.*)$ index.php?page=profile&username=$1 [L,NC]
The path is getting redirected properly, but the css,flash and images are not loading. Also if I use the following '/' after any url say,
RewriteRule signup(/?)$ index.php?page=signup [L,NC]
It's not finding any page there, i.e. error 404. I just want my htaccess file to work for both /signup and /signup/
When browser displays:
http://example.com/coaster/CoasterInsider/signup
http://example.com/coaster/CoasterInsider/signup/
and encounters a relative URL such as:
<img src="site/images/photo.png">
It translates the relative URL to (respectively):
http://example.com/coaster/CoasterInsider/site/images/photo.png
http://example.com/coaster/CoasterInsider/signup/site/images/photo.png
You should use absolute URLs for assets. Make this a habit:
<img src="/site/images/photo.png">
Alternately you can use the HTML base tag in your pages which tells browsers how to treat relative URLs. Personally I do not recommend it.
This was advised to, and worked for me, place it inside the <head> tags in your html: <base href="/"> Again, it worked for me, I hope it helps someone else.
Use
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
to exclude real files and directories from being rewritten.
You could also add
RewriteCond %{REQUEST_FILENAME} !-l
to do the same for symlinks.