Htaccess when put '/' in the end of url - apache

Im doing a website with only one page and I want do put in url like: "http://test.com/portfolio" and my htaccess to access the same page like http://test.com/#portfolio, I already get this with the code below, but when I put http://test.com/portfolio/, with '/' after, the server open the same page, but without css, links with images... The browser show only the html code.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^portfolio(.*)$ #portfolio
RewriteRule ^contact(.*)$ #contact
</IfModule>
You know something to help me?

This is because you css and images are dynamically linked (doesn't start with a /). This means when you go to:
/portfolio/
The relative URI base changes from / to /portfolio/. Which means if you had an image linked like:
src="images/foo.jpg"
instead of loeading /images/foo.jpg, you load /portfolio/images/foo.jpg.
Either change your links so that they're absolute links (starts with /) or add this to the header of your page:
<base href="/" />

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.

Why can't I access my page and query params after htaccess rules rewrite the url?

I have a url of the form --> https://websitename.com/content?id=xyz
I want to rewrite the url in the form --> https://websitename.com/content/xyz
I want to be able to access the query param (id) and its value (xyz) on the content page after the url has been rewritten
All pages are *.php pages
I've used the following htaccess rewrite rule
RewriteEngine On
# Hide .php file extension in the urls
RewriteRule ^([^/.]+)$ $1.php
RewriteRule ^content/([A-Za-z0-9-]+)/?$ content?id=$1 [NC,L]
When I navigate to the rewritten url, it doesn't work and I get the following error:
"Not Found
The requested URL was not found on this server"
I also can't access the query parameter and its value on the rewritten url
But, when I enter the original url, that works and I can access the query parameter and its value
I'd appreciate it a ton if someone can help me find out what I am doing wrong and how I can fix this.
Thanks,
Dexxterr.
With your shown samples, please try following once. Please clear browser cache before testing your URLs.
RewriteEngine On
# Hide .php file extension in the urls
RewriteRule ^([^/.]+)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^content/([A-Za-z0-9-]+)/?$ content.php?id=$1 [NC,L]
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.

URL Rewriting Add a folder name to URLs

I have an apache2 server.
My directory structure is - (below is inside - /some-folder/abc)
- app
- otherfolder
- pqr.php
- xyz.php
- js
- css
.htaccess is placed inside /some-folder/abc
Context root for virtual host is set till - /some-folder/
Now, I want my users to enter this URL - http://someserver.com/abc/xyz or http://someserver.com/abc/pqr
I want to open the page at - http://someserver.com/abc/app/xyz.php or http://someserver.com/abc/app/pqr.php
How can I achieve this using URL Rewriting mod_rewrite
This is what I have so far, which is not working -
Options +FollowSymLinks -MultiViews
#Turn mod_rewrite on
RewriteEngine On
#RewriteBase /abc
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /app/$1\.php [L] # this doesn't work either
# RewriteRule ^(.*)$ app/$1.php [L] # this doesn't work either
# RewriteRule ^abc/(.*)$ abc/app/$1.php [L] # this doesn't work either
Thank you for your help.
If possible, I would also like to take all query params using forward slash
ex - http://someserver.com/abc/app/xyz.php/xId/123/uId/938/sdh/8374 instead of http://someserver.com/abc/app/xyz.php?xId=123?uId=938?sdh=8374
There is no pattern for query params, they can be anything for an page. Is this possible by a generic mod_rewrite, or do I have to rewrite urls for each page, and each time my developers add a new query param?
This rule should work for you from /abc/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/abc/app/$1.php -f
RewriteRule ^(?!internal)(.+?)/?$ app/$1.php [L,NC]
RewriteCond makes sure that we have a corresponding .php file in /abc/app/ sub-directory.
(?!internal) is negative lookahead assertion to skip internal from this rewrite.
Also it appears you're using a relative URL for css/js/images e.g. src="abc.png" and your current URL is: /abc/xyz then browser resolves this relative URL to http://example.com/abc/xyz/abc.png which obviously will cause a 404 since your static files are residing in /abc/app/ sub-directory.
You can add this just below <head> section of your page's HTML:
<base href="/abc/app/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
Also as a practice to use absolute links instead of relative ones change relative link XYZ, to this one XYZ

Problems redirecting from a 403 using .htaccess (Yourls)

I'm using a php app called Yourls. It's a self-hosted url shortener and it's pretty great, I'm happy with its overall functionality. Due to the nature of its development however there isn't much in the way of support. Let's pretend the base url is af.to, where a shortened url would be af.to/goo that might redirect to whatever url is defined by 'goo'. The problem I'm facing is that if someone goes to af.to, they end up on a 403-Forbidden. I'd rather the client is redirected to a specific url instead. I have already picked up a plugin for Yourls which redirects to a url when a shortlink is not found or mis-typed, but this does not cover the base of af.to
I attempted to put in a 403 redirect in the .htaccess, but that broke the whole yourls script resulting in a 500 server error.
Current .htaccess looks like this:
# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /yourls-loader.php [L]
</IfModule>
# END YOURLS
Any help on what I need to do?
Thank you.
The RewriteCond blocks tell the RewriteRule to skip existing files / folders. When you go to http://af.to/, the root folder exists : no redirection. The apache server doesn't find any index.html (or index.php) file, isn't allowed to list the content of the folder, give up and returns a 403 Forbidden.
You can create the index.html file to show some content or you can add these lines to redirect to an other url :
# just after RewriteEngine On
RewriteRule ^/$ http://my-compagny.com/ [L,R=301]

Clean URL not working properly

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)