RewriteEngine On
RewriteBase /games4u/
RewriteRule ^index/?$ index.php [NC,L]
RewriteRule ^forum/([a-zA-Z0-9_-]+)$ forum.php?user=$1
RewriteRule ^forum/([a-zA-Z0-9_-]+)/$ forum.php?user=$1
So what I want is the url to be: localhost/forum/1
1 = user id 1. But the design wont load because of this. It's like the CSS file gets removed or something. Why doesn't thios work?
When you add extra / slashes in the URL, that changes what the relative URL base becomes. The browser doesn't know that /games4u/ is the base for relative URL's generated by the forum.php code. Now that you've added another level of folders, /games4u/forum/username, the browser attempts to resolve relative URLs by adding the base: /games4u/forum/ to all the links. You need to specify your base in the page headers or make all your links absolute (starts with /games4u/):
<base href="/games4u/" />
The RewriteBase directive has nothing to do with the relative URL base on the browser. The browser doesn't even know you're using rewrite rules. The RewriteBase directive is only for relative paths in your RewriteRules.
You need to set conditions for your re-writing, to ignore folders and files:
RewriteEngine On
RewriteBase /games4u/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index/?$ index.php [NC,L]
RewriteRule ^forum/([a-zA-Z0-9_-]+)$ forum.php?user=$1
RewriteRule ^forum/([a-zA-Z0-9_-]+)/$ forum.php?user=$1
Also, it's recommended that you will use the full path for your css / js files, such as:
<script src="http://www.example.com/js/file.js"></script>
Related
I am using .htaccess code to turn blog.php into /blog/ with this code:
# --- CUTENEWS[ST]
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^blog(\d+)*$ ./blog.php
RewriteRule ^blog/(\d+)*$ ./blog.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /blogengine/show_news.php?cn_rewrite_url=$1 [L]
# --- CUTENEWS[ED]
This works fine, except when I load the web page with /blog/, the images break. If I load the web page with /blog.php or /blog, the images load fine.
I have been searching through all the .htaccess issues people have had on Stackoverflow, and this is as far as I have come to getting things working. The cutenews php is there because I am using cutenews for blog integration in my site.
I appreciate any suggestions. I am pretty new to .htaccess
This is because your relative URIs have their base changed. Originally, the base is / when the page is /blog.php, and the browser properly fills in relative links with the / base. But when the browser goes to a page like /blog/ the base suddenly becomes /blog/ and it tries to append that in front of all relative URLs and thus none of them load.
You can either make your links absolute, or change the URI base in the header of your pages (inbetween the <head> </head> tags):
<base href="/">
You are not accounting for trailing slash. This RewriteRule ^blog(\d+)$* will not match /blog/ .Try this instead:
RewriteRule ^blog/([0-9]+[^/])*$ /blog.php/$1
EDIT:
As we discussed in chat you needed something else, here it is:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog(.*)$ /blog.php?route=$1 [R=301,L]
This will catch URLS like: /blog, /blog/, /blog/anythig_goes_here/2343/ . Adding 301 Permanent Redirect header, and L meaning don't process other rules.
in your blog.php you can reach 'route' paremeter via:
echo $_GET['route'];
EDIT:
After discussing further, the issue was solved by removing "/" from the begining of the redirect url, so now that rewrite rule line looks like:
RewriteRule ^blog(.*)$ blogposts.php?route=$1 [L]
For images, absolute urls need to be used, starting with "/" pointing to your web root.
In my .htaccess file I rewrite all URLs to my index file like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?string=$1 [L,QSA]
So far, so good. When I request the url example.com/path/to/my/page the value of $_GET['string'] is path/to/my/page, as it should be.
Now the problem:
in my index.php I use several script/css files like this:
<script src="script/script.js"></script>
With these rewrite rules on, the script/css files get requested as example.com/path/to/my/script/script.js.
Is there a way to make sure the script gets requested without the /path/to/my part?
The only solution I found is putting the entire url in the src attribute, but I don't like it that way.
Note: the path part can consist of a variable number of parts, so /long/path/to/another/page or /page are also possible URLs.
You can also add a relative URL base to the header of your index.php file:
<base href="/" />
I am having some trouble with Htaccess and RewriteRule.
Basically:
I have a directory called /_dev/WEBDEV and want people to access it from /WEBDEV on the server.
(http://domain.com/WEBDEV will point to http://domain.com/_dev/WEBDEV)
Here is the htaccess code I have used
RewriteRule ^WEBDEV(|/)$ /_dev/WEBDEV/
RewriteRule ^example(|/)$ /_dev/example/
So when i go to http://domain.com/WEBDEV, the page shows, but there is no CSS styling or images.
What i mean:
<img src="img/shape3.png">
the url on the server would be /_dev/WEBDEV/img/shape3.png
but with the Htaccess it gives
http://domain.com/img/shape3.png
as the image (and throws a 404).
Plus, i have a file on my server /_dev/WEBDEV/app1.php and with the htaccess it gives a 404 not found.
First, define your RewriteBase correctly
RewriteEngine On
RewriteBase /
Your rewrite rule should not be triggered if the requested url matches a file or directory.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
And you'll need to add the rule's REGEXP match to the rewritten path
But first, rewrite the path to the WEBDEV directory so it always have a slash after it
RewriteRule ^WEBDEV$ WEBDEV/ [L,QSA]
RewriteRule ^WEBDEV(.*)$ /_dev/WEBDEV$1 [L,QSA]
RewriteRule ^example(.*)$ /_dev/example$1 [L,QSA]
This is quite like creating aliases, but it can be done in .htaccess
For external resources like css and html, you just have to change their paths to
<img src="/img/shape3.png">
for example. This way the absolute path is calculated from the domain name (or base path), resulting in yourdomain.ext/img/shape3.png which should work.
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.