I have a small problem with rewriting urls. I have this really simple rewrite rule:
RewriteEngine on
RewriteRule ^(home|apps|news|research|contribute)/$ index.php?page=$1
However, whenever I call my website like this, e.g. website.net/home/ it correctly rewrites the rule and shows the corresponding page but also messes up the css and images since it rewrites the urls for them since they are relative paths in the code.
Is there any easy way to prevent this from happening?
This is because of the rewritten urls. when the url is example.com/home/ apache thinks /home/ is a directory and appends /home/ in front of all relative urls.
To solve this, You can add the following base tag in head section of your webpage :
<base href="/">
Related : Seo Friendly Url css img js not working
Add this following line in your .htaccess file
RewriteRule \.(gif|jpg|css|js|inc\.php)$ -[L]
Related
Hey I'm struggling with this all day long and I'm new to clean URLs.
My Domain which i want to change:
http://www.domain.tld/ausflug.php?lang=de
to:
http://www.domain.tld/ausflug/lang/de/
via .htaccess. This is my code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ausflug/lang/(.*)/ ausflug.php?lang=$1
My Problems are:
If I navigate through my page, the url still is the ugly one.
And if I want to reach the page with the clean url, it won't display all the
css and js stuff.
Thanks for help!
First of all, replace your current code by this one (your htaccess should be in root folder)
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/ausflug\.php\?lang=([a-z]{2}) [NC]
RewriteRule . /ausflug/lang/%1/? [R=301,L]
RewriteRule ^ausflug/lang/([a-z]{2})/$ /ausflug.php?lang=$1 [L]
This will redirect domain.tld/ausflug.php?lang=de to domain.tld/ausflug/lang/de/ and will internally rewrite it (without any redirection loop error) to domain.tld/ausflug.php?lang=de.
For your second problem:
if I want to reach the page with the clean url, it won't display all
the css and js stuff
Simply use absolute paths for your links (with a leading slash).
Example: /css/file.css instead of css/file.css (otherwise it will look in wrong directory, since your rule is generating some virtual directories)
Or you can add this html tag <base href="/"> right after <head> in all pages (instead of replacing each links)
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^$ http://example.com/index.php?flash=1 [L,R=301]
I have a page where I want to rewrite my base url, but I have some problems. I have the above in my .htaccess file but still the Rewrite will not take effect or work and Rewrite to index.php?flash=1 when users land on my page, what could the problem be?
In short what I want to do is that when someone lands on my page example.com/ the url should be example.com/index.php?flash=1
short: i want to change my root url when someone visits my page to something else, how?
This also has to do with mod_rewrite and the above question, is it searchengine friendly? if not how can i make it ? from what i heard with the url i use above searchengines with not index anything after the questionmark, what would be your suggestion to change if i want searchengines to index my pages ?
It sounds like you can use just a standard re-direct instead of dealing with re-writes.
place an index.html file in your root with this line in it:
<meta http-equiv="REFRESH" content="0;url=index.php?flash=1">
The index.html file should be viewed before index.php would be, then it just does a redirect.
If not place <meta http-equiv="REFRESH" content="0;url=index2.php?flash=1"> at the top of index.php and just make a new index2.php file to display the content you want.
It's a work around, but it's a quick fix.
I have an interesting issue that I can't seem to get through. We have a URL that has been setup to route all requests to that URL (via nginx on another server) to my Codeigniter app (that exists on another server). For example, the URL is like this:
http://www.site.com/myappname/controller/function/parameter
Under normal conditions in Codeigniter, it routes the first element after the trailing forward slash to a controller class, then the second to a function within the class, and the third/etc as paramaters of that function.
What I need it to do is to disregard the "myappname" in the URL and have it send through the URL without that. The "myappname" is NOT a folder on the server.
I'm not 100% sure if I need to add new HTACCESS rules, or if I can do this programmatically through Codeigniter.
My current HTACCESS file:
RewriteEngine on
RewriteCond $1 !^(index\.php|sitemap\.xml|robots\.txt|public|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
This is a basic, and straight forward HTACCESS setup for all Codeigniter apps, however, using this, and using the structure above, if I go to, http://www.site.com/myappname, with "http://www.site.com/myappname" set as my base_url in the Codeigniter config, Codeigniter then translates it as:
http://www.site.com/myappname/myappname
So...can anyone help? I also tried to add the following code to the top of my config/routes.php file:
$_SERVER['REQUEST_URI'] = str_ireplace('/verified', '', $_SERVER['REQUEST_URI']);
That actually works partially, but now when I request CSS/JS files, from the root, it doesn't pull those in correctly because I use the built in site_url() function to build dynamic links based on the site's "base_url" property. I don't think I can do this 100% programmatically inside Codeigniter (through overwriting system level functions) because my CSS/JS/etc exist in a /public folder in the root of my app (that's obviously excluded from routing via my RewriteCond).
Anyone have a suggestion?
Try adding a RewriteBase to your htaccess:
RewriteEngine On
RewriteBase /myappname/
RewriteCond $1 !^(index\.php|sitemap\.xml|robots\.txt|public|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
I am working on a website created using PHP.
Development environment is Windows (using xampp)
The pages of website are displayed according to the value of the "page" variable that we pass to the index.php file as a url variable.
i.e. http://example.com/index.php?page=xyz
The url above points to the xyz.php file
There are around 50 pages (can be increased in future).
I want my urls to be like this
http://example.com/xyz
for this to work I wrote the following .htaccess rule in the .htaccess file (in the root folder of project)
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]
But it is not working at all.
Another url is
http://example.com/index.php?page=abc&name=gaurav&id=10&page_no=2
I want this url to be shown like this
http://example.com/abc/gaurav/10/2
There are many different forms of the url that I took example of but the page variable will always be there in the URL.
If I provide the rule for both the above urls then nothing works (500 internal server error is shown)
Please help me write this rule.
Try
RewriteRule ^([^/]+)$ index.php?page=$1 [L]
So a + instead of a star. It will use all characters till he finds a forward slash (/)
I am struggling with .htaccess rewrite rules.
Let's say I have this URL.
localhost/site/index.php
and I want to rewrite it as this URL
localhost/site/tutorial
I would use this RewriteRule
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^tutorial/(.*)$ /up/index.php
The page works, but the CSS files don't load.
Also, if I have a URL like this:
index.php?page=home
Then I would have to parse through that URL to get 'home' not using $_GET anymore correct??
Just use absolute URLs for your CSS file, or at least reference from domain root.
http://www.mysite.com/css/myCssFile.css
or
/css/myCssFile.css
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^tutorial/(.*)$ /up/index.php
This will rewrite anything that goes to the tutorial directory as /up/index.php, which means that unless you host your CSS file at that location, it won't work. If your css file is at /tutorial/css.css, it will redirect it to /up/index.php
You would have to parse through the %QUERYSTRING% in order to redirect based on index.php?page=home
So the CSS files are not loading because you are changing the location of the served file.
So if you are linking to ./css/style.css => ^/tutorial/css/style.css is not the same as /up/css/style.css
And you can retain the get if you rewrite:
RewriteRule ^tutorial/(.*)?(.*)$ /up/index.php?$2