I want example.com/video/ - with or without last slash - to be interpreted as example.com/video.php - and this works
example.com/video/5/lorem-ipsum/ - with or without last slash - should be interpreted as example.com/video.php?id=5&s=lorem-ipsum
it works only without last /
with that slash - the content is there but css is missing
please help
<base href='../../'>
RewriteRule ^video/?$ video.php [L]
RewriteRule ^video/([^/]*)/(.*)/?$ video.php?id=$1&s=$2 [QSA,NC,L]
With your shown samples please try following htaccess rules file. Please make sure that your .htaccess file is residing along with your video.php file. Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(video)/(\d+)/([^/]*)/?$ $1.php?id=$2&s=$3 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(video)/?$ $1.php [QSA,NC,L]
JS/CS rewrite/redirect:
You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.
Related
My gosh - I hate .htaccess ... and don't understand how the hell anything is working.
I am trying to ignore 'subfolders' and use these as guest identifyer.
Ex:
User types > http://subdomain.domain.com/guest_id
Server sends > http://subdomain.domain.com/index.php
Browser shows > http://subdomain.domain.com/guest_id
My htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [NC]
The code works great... unless there is a trailing slash.
In this case, all files are served as 'index.php'.
The main Index.php is showing properly, but all external scripts / css / etc. are served as /guest_id/index.php (thus... do not load).
FYI, there might be a conflict as my 'subdomain' is actually invisibly serving domain.com/event/
I have been trying so many permutations... without really understanding how htaccess work.
Your help will be sooooo much appreciated.
1,000 thanks in advance!
Damien
When you have URL such as http://subdomain.domain.com/guest_id/ and are using relative links for your css/js/images then browser attempts to resolve all links by prefixing them with current path hence src='image.png' is resolved as http://subdomain.domain.com/guest_id/image.png instead of http://subdomain.domain.com/image.png.
You can have a rule to force remove trailing slash at the end of all non-directory URLs.
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Also you can add this just below <head> tag of your page's HTML:
<base href="/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
I'm trying to make SEO Friendly url by altering php parameters with slash based url.
I want to make
mysite.com/TopList.php?tl=ToplistName&fr=2021-02-10&to=2021-02-20
into:
mysite.com/ToplistName/2021-02-20/2021-02-20
I had success with rewriting url but none of my includes are referring to right directory path and I get no css, js and file paths are broken from links.
This is what I have in .htaccess file now:
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ TopList.php?tl=$1&fr=$2&to=$3
Anyone that can help me get this sorted out inside .htaccess file?
Based on your shown samples, could you please try following.
Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(\d{4}-\d{2}-\d{2})/(\d{4}-\d{2}-\d{2})/?$ TopList.php?tl=$1&fr=$2&to=$3 [L]
I solved this by doing following:
added to html:
<base href="http://example.com/">
added to .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
#1 Changing Full query
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ TopList.php?tl=$1&fr=$2&to=$3
#2 Changing only first landing page
RewriteRule ^([^/.]+)/?$ TopList.php?tl=$1
this gave me following result:
mysite.com/TopList.php?tl=ToplistName ==> mysite.com/ToplistName
mysite.com/TopList.php?tl=ToplistName&fr=2021-02-10&to=2021-02-20 ==> mysite.com/ToplistName/2021-02-10/2021-02-20
So i'm trying to rewrite the URL like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?page=$1 [QSA]
Works perfectly but relative paths to JavaScript and CSS get messed up.
Shouldn't the line below exclude files ?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-f makes sure existing files are not rewritten hence images/css/js files won't be routed to index.php.
However your problem seems to be use of relative paths for images/css/js. Make sure 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 /.
Otherwise you can add this in the <head> section of your page's HTML:
<base href="/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
I have a website running at localhost/pm and the RewriteBase is correctly set to /pm/. There is a link tag in my document: <link ... href="themes/default/css/default.css">.
When the url is localhost/pm or localhost/pm/foo the CSS works all right. When there are more slashes in the URL, however, like localhost/pm/foo/bar the relative URL if the stylesheet changes to foo/themes/default/css/default.css.
How do I get this to work without having to put some sort of PHP path resolution in the link tag?
# invoke rewrite engine
RewriteEngine On
RewriteBase /pm/
# Protect application and system files from being viewed
RewriteRule ^(?:system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
EDIT:
Basically what I need now is this:
If request contains folder name /themes/ scrap everything that is before /themes/ and rewrite the rest to /pm/themes/...
I tried it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/$1 but I get an internal server error. Why?
If I do it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/ (ie. just remove $1 from the end) and use the URL http://localhost/pm/foo/themes/foo/ the resulting physical location is http://localhost/pm/themes which is what is expected too, which in turn means that at least my regex is correct. What am I missing?
The RewriteRule is almost correct
RewriteRule ^.*(/themes/.*)$ /pm/themes/$1
This rewrites http://localhost/pm/foo/themes/default/css/default.css to http://localhost/pm/themes/themes/default/css/default.css, which is one themes too much. Use this instead
RewriteRule /themes/(.*)$ /pm/themes/$1 [L]
But now you have an endless rewrite loop, because /pm/themes/.. is rewritten again and again. To prevent this, you need a RewriteCond excluding /pm/themes
RewriteCond %{REQUEST_URI} !^/pm/themes/
RewriteRule /themes/(.*)$ /pm/themes/$1 [L]
Now the request is rewritten only once and you're done.
You probably need to add the following lines before your RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
It will only evaluate your rewrite rule if the requested file or directory doesn't exist.
You should post your .htaccess file so we can offer better advice
I want to to use flat links like this:
http://somedomain.com/about-us
http://somedomain.com/products-list
http://somedomain.com/product/item
Thus I've used the mod_rewrite rules like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9A-Za-z-]+)?$ /index.php?page=$1 [L]
RewriteRule ^([0-9A-Za-z-]+)/([0-9A-Za-z-]+)?$ /index.php?page=$1&value=$2 [L]
</IfModule>
The first two links are working fine. But whenever I visit the third type of links the images or the css or any js script that are linked as relative path eg. <img src="images/image.jpg"> and About Us.
The browser thinks it is in http://somedomain.com/product/images/image.jpg and http://somedomain.com/product/about-us.
The correct path should be http://somedomain.com/images/image.jpg and http://somedomain.com/about-us
And thus files with relative links are not working.
How to fix this issue? I don't want to use full path instead of relative for all linked files. Please suggest a way by doing some tweaks in the .htaccess
Try escaping your slash:
RewriteRule ^([0-9A-Za-z-]+)\/([0-9A-Za-z-]+)?$ /index.php?page=$1&value=$2 [L]