absolute links and mod_rewrite - apache

I want to use mod_rewrite and I set everything up to use "absolute links" in order to overcome URL resolving issue. Now my links look like (/file.php) instead of (./file.php) and everything works ok on localhost.
Unfortunately, when I put the project on my actual server, links are not valid anymore so I want to know where "/" is pointing to and how to resolve this issue.
(I do not want to use html base tag at this point.)
localhost: http://project.local
Actual host: http://project.com
And here is what I have in my .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^page/([0-9-]*) /home.php?page=$1 [QSA,L]

try using rewritebase
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^page/([0-9-]*) /home.php?page=$1 [QSA,L]

Related

.htaccess simple mod_rewrite sends 404 error

I've created an .htaccess file in a shared server, inside a folder called API that is a child of the root folder.
I've placed the following simple code inside the file
Options +FollowSymLinks
RewriteEngine On
RewriteBase /API/ # also tested with / alone (edit) also tested removing-it
RewriteRule ^/API/(\d+)*$ /API/index.php?i=$1
My intentions is to turn /API/{var} into /API/index.php?i={var}, but the trick ain't working at all. What can be causing the issue, since every query var I sends me into 404.
What can be causing the problem? Where should I start debugging?
Edit:
After several failed attempts I'm gonna try using the FastRoute php library as an alternative, since this .htaccess issue seems unresolvable.
Thanks Félix for all the help in the chat.
The problem is probably the leading slash / and API in your rewrite pattern, Remove them from there
Try :
Options +FollowSymLinks
RewriteEngine On
RewriteBase /API/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\d+)*$ /API/index.php?i=$1 [NC,L]

.htaccess is not rewriting my url

i'm quite new to .htaccess file and I nees a bit of help..
I have a Greek site and I want to have urls like επικοινωνία.html. However, I haven't managed to translate successfully using htaccess file.
My code, for example, is:
Options +FollowSymlinks
RewriteEngine On
RewriteRule επικοινωνία.html contact.html
However, the url showing in the browser is not changing at all.
What am I missing?
Try this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^επικοινωνία\.html$ /contact.html [L,B,R]

mod_rewrite 404 error trying to rewrite URL

I have website that is using unfriendly URLs and I want to change them using mod_rewrite.
I have a URL like this:
http://www.website.nl/?p=2
and I want it to be
http://www.website.nl/about-us
When I use this on my local server it works correct but on the live webserver it doesnt
I use this code in my .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^about-us$ /?p=2 [L]
I already checked if mod_rewrite is enabled and in my apache2handler it is enabled so it should work shouldn't it?
What am I missing or doing wrong?
You may be missing the leading / (forward slash) in your pattern, try:
RewriteRule ^/about-us$ /?p=2 [L]

Apache - rewrite images to php file with .htaccess

I'm looking for a way to rewrite all my image requests from one folder into some.php file, while preserving the original image url (or partial path).
So,
example.com/folder/img/test.jpg
would be rewrited as something like
example.com/folder/some.php?img=img/test.jpg
(is this the best approach?)
I'm not familiarized enought witrh regular expressions, so I'll be very thankfull :)
note : I've tried some solutions before, none of them worked. ALso, I'm running Apache 2.0 under CentOS environment.
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 ^(folder)/(img/[^.]+\.jpg)$ $1/some.php?img=$2 [L,QSA,NC]
Make sure:
.htaccess is enabled
mod_rewrite is enabled
Your URL is http://example.com/folder/img/test.jpg
It sounds like you you want the filename of the image in the url to be included in the new php url, not the entire url. So something like:
RewriteRule ^folder/img/(.*[.]jpg)$ /folder/some.php?filename=$1
Considering what you mention in the comments and that the previous rules didn't work, I edited the message, this is what i have now.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)\.jpg [NC]
RewriteRule ^folder/img/([\w]*\.jpg)$ folder/some.php?img=img/$1[R=301,L]
If folder is al variable, you can change that for (\w*) and add the reference in the right side of the rule.
Hope this helps.
Bye

How can I add an additional word to a URL using mod_rewrite

I have an application with URLs such as these:
https://www.domain.com/example/public/subscription
https://www.domain.com/example/public/subscription/source
https://www.domain.com/example/public/test/subscription
I'd like to use mod_rewrite (or some other method) to create shorter "aliases" of the above URLs by removing the /public/ part so that I can provide my client with these shorter versions of the URLs:
https://www.domain.com/example/subscription
https://www.domain.com/example/subscription/source
https://www.domain.com/example/test/subscription
In other words, when browsing to https://www.domain.com/example/subscription, for example:
The server must send back the same response that one would get when opening https://www.domain.com/example/public/subscription directly
The browser must still display the shorter version of the URL (without the /public/) in the address bar
Is this even possible, and how would such a RewriteRule look like?
Thank you in advance.
put this is .htaccess file in your DocumentRoot.
Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^(example)/(.*)$ $1/public/$2 [NC,L]
You just want to "remove" the public keyword?
Try this rewrite rule:
RewriteRule ^/example/public/(.*) /example/$1 [L]