Mod Rewrite - wrong links - apache

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.

Related

HTACCESS rewrite website/product.html?productid=P01 to website/product/P01

how i can rewrite this url
website/product.html?productid=P01
to
website/product/P01
with .htaccess?
(P01 is a variable ID)
i try this but din't work
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9]+)/?$ product.html?productid=$1 [L,QSA]
You can have your rule as this:
Options -MultiViews
RewriteEngine on
RewriteRule ^product/([\w-]+)/?$ product.html?productid=$1 [L,QSA,NC]
Also make sure to add this just below <head> tag of your page's HTML: <base href="/website/" /> so that every relative URL is resolved from that base URL and not from the current page's URL.

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

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)

Htaccess when put '/' in the end of url

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="/" />

HTACCESS how to mod_rewrite the website Asset File paths?

With Apache, how to make a URL (lets say an Image Path) to be actually coming from another location. For example, i have a html page with an URL like:
apple.html, with the original codes inside:
<img src="http://www.myoldsite.com/assets/image/apple.jpg" />
<img src="http://www.myoldsite.com/assets/image/orange.jpg" />
But actually i want this path to be re-written to:
<img src="http://www.mynewsite.com/image/apple.jpg" />
<img src="http://www.mynewsite.com/image/orange.jpg" />
So when i access the apple.html page, the image should be coming from new location, without needed to go and change the actual codes inside the files each.
But actually i'm totally weak in .htaccess rules. When i used like following, it is showing error:
RewriteEngine On
RewriteRule http://www.mynewsite.com/image/(.*) /assets/image/$1 [QSA, L]
I am having thousands of pages like this but I am totally stuck with .htaccess rules.
What am i missing please? Please help.
I assume that both domains point to the same apache instance?
You seem to have switched the arguments for the RewriteRule. The syntax is:
RewriteRule UrlsMatchingThis ChangeToThis [params]
So in your case I would do something like this:
RewriteRule ^/assets/image/(.*) http://www.mynewsite.com/image/$1 [R,NC,L]
This matches all requests where the part just after the tld is /assets/image/(anything).
The flags I have used is
R redirect
NC Ignore case
L Last rewrite rule for this request
You may want to add a condition to the RewriteRule like this:
RewriteCond %{SERVER_NAME} ^(www\.)?myoldsite\.com$ [NC]
RewriteRule ^/assets/image/(.*) http://www.mynewsite.com/image/$1 [R,NC,L]
This will only rewrite your requests if the domain is myoldsite.com, with or without www.
Se http://httpd.apache.org/docs/current/mod/mod_rewrite.html for documentation of the rewrite module.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(image/.+)$ /assets/$1 [NC,L]