htaccess Rewrite rule issue for css with '/' - .htaccess

I have Rewrite rule as below.
RewriteEngine On
RewriteRule ^inner-subcategory-([0-9]+)$ /inner.php?subcat_id=$1
This rule is working fine. The URL will be like this
http://www/abc.com/inner-subcategory-10
But if I am using '/' instead of '-', CSS can not be applied.
This means, I want a URL like http://www/abc.com/inner/subcategory/10

To use slashes instead of hyphens, replace - by /
RewriteRule ^inner/subcategory/([0-9]+)$ /inner.php?subcat_id=$1
The second part, rewriting the css folder, depends on the requested URL. When the main page is requested as /inner/subcategory/5, then the relative CSS URL will be requested as /inner/subcategory/css/jquery.simpleLens.css. This means, you must add another RewriteRule and rewrite the css folder to an absolute URL
RewriteRule ^inner/subcategory/(css/.+)$ /$1 [L]
Same goes for Javascript files or images.

Related

Rewrite URL through .htaccess

My website generates page URL with the prefix ?Script=, which is not SEO-friendly.
I want to remove this prefix and just keep the page title.
For example, I want to convert the following URL:
example.com/?Script=about to example.com/about
about is the About page of my website.
I want to set up a rule for omitting ?Script= for all pages.
I have managed to do it for my blog post by using the following rewrite rule.
RewriteRule ^news/([0-9a-zA-Z_-]+) ?Script=news_view&uid=$1
This should work for you .
RewriteEngine on
RewriteRule ^(about)/?$ /?script?=$1 [L]
If you have more pages , you can add those pages to the pattern
RewriteEngine on
RewriteRule ^(about|page2|page3)/?$ /?script?=$1 [L]

.htaccess redirect anything after "/"

I want to redirect anything after "yy/" to a file xx.php in the same directory.
RewriteRule $ xx.php
If the user enters http://DomainName/yy it will redirect to xx.php with complete css from include('header.php')
If the user enters http://DomainName/yy/ it will redirect to xx.php without complete css from include('header.php')
There must be something that is wrong with my .htaccess entry?
You can place this rule in /xy/.htaccess:
RewriteEngine On
RewriteBase /yy/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^xx\.php$ xx.php [L,NC]
For css/js etc 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 try adding this in your page's HTML header: <base href="/yy/" /> so that every relative URL is resolved from that URL and not the current URL.

css, images and vendor folders do not load when using htaccess rewrite

Below is the htaccess scripti am using:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^gecko/([^/]*)$ gecko.php?gecko=$1 [L]
Which changes http://localhost/geckology/gecko.php?gecko=Zilly (which loads css etc fine) to http://localhost/geckology/gecko/zilly which doesn't load the css etc fine as it looking in directories like this http://localhost/geckology/gecko/css/theme.css when it should be http://localhost/geckology/css/theme.css
The site is huge, so if possible i would like a htaccess way of fixing this, however i will change everything to absolute urls if it's not possible
You can fix the relative URI base by simply adding this to the header of your pages:
<base href="/" />
or if you have to use htaccess, which is really inefficient and will assume all of your css/scripts/etc are all in one place, while at the same time making the rest of the world think the same thing is actually 2 different URLs:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^gecko/(css|images|other)(/.*)$ /$1/$2 [L]
This blindly rewrites any URL that tries to access "css", "image" or "other" folders within the /gecko/ path.

htaccess rewrite url with extra slash

I have this code for the htaccess file, and soon I'll have pages where I'd like to create a url like the bottom one
RewriteEngine On
RewriteRule ^Index/?$ index.php [NC]
RewriteRule ^Gallery/?$ gallery.php [NC]
RewriteRule ^Showreel/?$ showreel.php [NC]
RewriteRule ^Music/?$ music.php [NC]
RewriteRule ^Gallery/Render/?$ contact.php [NC]
If I go to the contact page, it'll display a page, but it's entirely white and only contains the text. I'm guessing it's to do with the images not being linked up properly with the new url, but how would I actually go about fixing this without having to manually edit the locations of each image?
You can try adding this in your page's header:
<base href="/" />
or change all of your links to absolute URLs. This issue is most likely that your links are all relative, and when you try to request a URL like:
http://yourdomain.com/Gallery/Render
The relative URI base becomes /Gallery/ instead of / (which is what it is if you access /contact.php directly). And when the browser tries to resolve all the relative links on the page, it uses the wrong URI base.

HtAccess: Basic ModRewrite

Given urls like this:
mysite.com/index.php
mysite.com/page/member/lobby.php
mysite.com/page/videos/video1.php
How can I rewrite the urls with .htaccess to hide the /page/ folder when it's present?
So the end result is:
mysite.com/index.php
mysite.com/member/lobby.php
mysite.com/videos/video1.php
You can use this rule to add page/ to your path internally:
RewriteCond $1 !=page
RewriteRule ^([^/]+)/.+ page/$0 [L]
Now every request that’s URI path has at least two path segments but that’s first segment is not page will be prefixes with /page. So /member/lobby.php will be rewritten to /page/member/lobby.php.

Resources