Clean URL not working properly - apache

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)

Related

Mod Rewrite - wrong links

In .htaccess I have:
RewriteEngine on
RewriteRule documentation documentation.php?id=0
RewriteRule documentation/process documentation.php?id=1
when I open https://example.com/documentation/process correct website is called (documentation.php?id=1) but because I have links like "assets/css/xxx.css" instead "https://example.com/assets/css/xxx.css" my web site is not working correctly - basically it is pointing to wrong directories.
How can I fix that without changing all links to "https://example.com/assets/css/xxx.css"?
Have it like this with anchors on both sides, using appropriate flags and MultiViews turned off:
Options -MultiViews
RewriteEngine On
RewriteRule ^documentation/?$ documentation.php?id=0 [L,QSA,NC]
RewriteRule ^documentation/process/?$ documentation.php?id=1 [L,QSA,NC]
Also you must 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.

Mod rewrite condition for subdomain with pass through

I wanted to run this by you because I'm attempting to perfect an Apache mod rewrite rule, which works, but I'm going insane because my file paths are breaking.
Basically, I've set up a mod rewrite rule to redirect a subdomain, to a subdirectory in my docroot which contains a static index.html file. Existing rules are listed as follows:
RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteRule ^(.*)$ /subdirectory/$1 [PT]
The redirect works fine. What happens though, is my images are breaking. For example, '/subdirectory/img', with the following img tag, doesn't work for me:
<img src="img/some-image.png" />
Same thing happens when I move the image to root, and attempt to call the image directly minus the 'img/' path.
Does my redirect have anything to do with this? Or am I missing something? I was under the impression that using the Pass Through [PT] flag would mean that asset linking would work.
Any advice is greatly appreciated. Thanks for your help!
Mark.
This is because you are using a relative url path for your images. Either use an absolute path starting with a / or add the following base tag in head section of your webpage :
<base href="/">
Related :
Seo Friendly Url css img js not working
Issue resolved. The rule works perfectly, it was just the positioning in the .htaccess file. I moved it directly underneath the following:
RewriteEngine On
Images and JS assets are now loading fine. I also added a missing 'L' flag.
Final working code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteRule ^(.*)$ /subdirectory/$1 [PT, L]

htaccess on Localhost refers to root, not folder

I am working on a website to make the URL's more SEO friendly with the htaccess, however, i bump into a problem when I set up the htaccess file. If I use the URLs I prefer to setup, i get refered back to the localhost and not towards the development map I am working in, so in this case, i get refered back to localhost/blog for example instead of localhost/jellyfish_rework/blog, so I end up with a view from my localhostor simply an error in the page.
The original link was http://localhost/jellyfish_rework/index.php?p=blog
Options +FollowSymLinks
RewriteEngine On
RewriteBase /jellyfish_rework/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /?p=$1 [L]]
Does this have to do with the fact my anchors refer to <a href"/blog"></a>? Cause in the other anchor tags that I still refer to in the old way, I dont get the responds back I want (so the link stays http://localhost/jellyfish_rework/index.php?p=blog)
edit
the htaccess file is located in the localhost/jellyfish_rework/ as that is where the index file is.
Your last line (RewriteRule) should look like this
RewriteRule ^([^/]*)$ index.php?p=$1 [L]
Don't add a leading slash before.
Otherwise it will be like an absolute path (/index.php from root).
With the rule above and your RewriteBase, this rule will act this way:
If not existing file/folder then rewrite it to /jellyfish_rework/index.php?p=URI
Your old rule (with leading slash) was about:
If not existing file/folder then rewrite it to /index.php?p=URI
Note: since you're using relative paths, you must also change all your html links (css, javascript, images, links) with a leading /jellyfish_rework/ or use the base tag
<base href="/jellyfish_rework/">

htaccess URL rewriting root url

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.

htaccess rewrite rule not loading site content

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