.htaccess pretty URLs are not working - php

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/" />

Related

Redirect all subfolders to a file in root

There are similar situations and answers here to redirect a url to a specific file using mod-rewrite. I tried them but none of them worked for me.
I would like to redirect all requests to a file and handle everything by that file. It worked fine somehow, but when there are virtual sub-folders, it does not work at all.
For example:
http://test.com/this_page_does_not_exist.php
works fine. but when I use something like this:
http://test.com/no_sub_folder_here/this_page_does_not_exist.php
it does not work fine. It tries to find css files for 404 page from no_sub_folder_here. So what I need to have is to address
http://test.com/no_sub_folder_here
to
http://test.com/
and then load the css for the 404 page. my css is in the root.
Here is my .htaccess file
RewriteEngine On
# Determine the RewriteBase automatically/dynamically
RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=BASE:%2]
# if request is not for a file
RewriteCond %{REQUEST_FILENAME} !-d
# if request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-f
# forward it to 404.php in current directory
RewriteRule . %{ENV:BASE}/404.php [L]
it does not redirect or change the directory from sub-directories inside 404 to root. It renders the codes, but the page style is all wrong because of missed css from
test.com/style.css
it checks
test.com/no_sub_folder_here/style.css
instead.
Also, after a couple of tries, it does not change the url form the no_sub_folder to root. So, after a while, I will get a long list of no_sub_folder in my url :(
Because you change the base path, you have to fix that, with:
<base href="http://test.com/">
in html <header> of your 404.php page.

url re write issue in .htaccess file

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

Access relative CSS/JS files with htaccess rewrite rule

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]

Mod_rewrite website like facebook

I'm using mod_rewrite for the first time in order o create a website similar to facebook.
Whener I type mywebsite.com/user.name, the mod_rewrite redirects me to mywebsite.com/hotsite/index/php and there, I use a little php to get the user name from the url and get the userId from it.
Then I have the other areas like mywebsite.com/user.name/diary, mywebsite.com/user.name/contact, and so on...
This is all working well with this code in my .htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /mywebsite
# ————————————————————————-
# > URL REWRITING
# ————————————————————————-
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)/diario$ hotsite/diary.php
RewriteRule ^(.*)/recados$ hotsite/messages.php
RewriteRule ^(.*)/fotos$ hotsite/photos.php
RewriteRule ^(.*)/videos$ hotsite/videos.php
RewriteRule ^(.*)/contato$ hotsite/contact.php
RewriteRule ^([a-z0-9._\-]+)$ hotsite/index.php [L]
The problem I have is with the path to the external files (css, images, backgrounds...).
Since my browser thinks I am in "website.com" I had to add "hotsite/" to all the paths. This works well for when I am at the user main page, like "mywebsite.com/user.name". However, if I go to "mywebsite.com/user.name/diary" the browser thinks I'm in another folder and then I have to add "../hotsite" in order for the paths to work.
I could make an IF in all the paths to check if I am at the index or not, but this would be very clumsy.
I could also put absolute paths in evertyhing, but since I'm developing offline with apache, this wouldn't be good either.
Any other solutions?
Thank you vey much.
You should probably use the base tag available in HTML (put it between the <head></head> tags):
<base href="yoursiteroot" />
In other words, something like this:
<base href="/mywebsite" />
However, this requires that your relative links are adjusted based on the path you specify. :-)

Rewrite rule for one image

I've got an .htaccess file that reads as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
There's an image on index.php that should always display, however when I navigate to a rewritten path, the request for the image is prepended with that path. So the "categories" section, while it should still display my foo.png at images/foo.png now has the path categories/images/foo.png
Can I fix this strictly with .htaccess rewrite rules? If so, how?
A more simple solution would be to use a src for the img-element in your html based on the root-folder of your site. (If it's just for that one image...)
For example:
http://www.site.com
<img src="/images/header.jpeg" />
This will always refer to http://www.site.com/images/header.jpeg, no matter what folder you're in.
This should take any request for the image and rewrite it to go the root /images/foo.png:
RewriteRule ^.*/images/foo.png$ /images/foo.png
Note that if you have a different image located at somewhere/images/foo.png, requests for that will also be rewritten to /images/foo.png, unless you write specific exceptions for it.
This sounds like a relative vs absolute URL mix-up. When you have paths like http://domain.com/category/post/name the relative URI base is /category/post and all relative links that get served in the content returned by the webserver will have that prepended to it by the browser. In order to tell the browser what the real URI base is, you can include this in your headers:
<base href="/">
Or you can simply change all your relative URLs to absolute URLs.

Categories