Friendly url in apache2 - apache

Now, have the next .htaccess (in the root folder):
Options -Indexes
Options -Multiviews
RewriteEngine on
# Ocultar extension php (hidden .php)
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# Url amigable (frienly url)
RewriteBase /
Rewriterule ^owner/servicios/(.+)/$ owner/servicios.php?servicio=$1
I expect when the user access try to "domain.com.ar/owner/index/" show the page "index.php". This not work, but, i access "domain.com.ar/owner/index" works.
On the other hand, when the user try to access "domain.com.ar/owner/servicios/servicio_uno/", $_GET value of "servicio" should be "servicio_uno", but is: "servicio_uno/.php/servicio_uno ".
And "domain.com.ar/owner/servicios/servicio_uno" shows 500 internal error.
Any ideas ?.

depending on where is your htaccess located (root directory or /owner directory)
//if in root
RewriteBase /
Rewriterule ^owner/servicios/(.+)/$ owner/servicios.php?servicio=$1
//if in owner directory
RewriteBase owner/
Rewriterule ^servicios/(.+)/$ servicios.php?servicio=$1
Hope it helps.

Try the following
RewriteEngine On
RewriteBase /owner/servicios/
RewriteRule (.*) /owner/servicios.php?servicio=$1
RewriteRule ^owner/servicios/servicios.php/(.*) servicios.php?servicios=servicios=$1 [QSA,L]
Hope this helps.

Related

.htaccess Remove filename from url

I'm trying to remove: "site.php" from my url:s, which look like
"example.com/custom-foldername/site.php", so the url:s would look like "example.com/custom-foldername"
Currently it's not working at all. Basically my site just doesn't change the url. I know that the .htaccess file is being read, because if i write junk code in it, it gives error 500.
Here is my current .htaccess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options -Indexes
RewriteRule ^site\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /site.php [L]
</IfModule>
The code you posted is a front-controller pattern - it rewrites every request for non-existent files to /site.php in the document root (not /custom-foldername/site.php).
And nor will a RewriteRule pattern like ^site\.php$ match a request like /custom-foldername if the .htaccess file is in the document root.
I'm trying to remove site.php from my URLs...
From all your URLs? Is site.php present in all your URLs? So, site.php is your front-controller?
If site.php really is your front-controller then try something like the following instead:
Options -Indexes
RewriteEngine On
RewriteBase /custom-foldername
RewriteRule site\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ site.php [L]
This rewrites everything that doesn't map to a physical file or directory to /custom-foldername/site.php.
UPDATE: I solved this my myself while ago, but forgot to update
So, i found the solution and here's my current .htaccess file, if someone is ever having the same problem
RewriteEngine On
RewriteBase /
Options -Indexes
DirectoryIndex site.php site.html
When user "asks" for folder directory (example.com/cats) apache looks for "site.php or site.html" files inside it. (example.com/cats/site.php) If it finds either on of them it displays it to the user as (example.com/cats). If either file isn't found it gives normal "404 not found error"

.html removal from URL using .htaccess rewrite rules

I have a static website - a bunch of static html pages. I am trying to remove .html part from the URL of my webpages. I have used an .htaccess file with the following code to do that:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
However, I am getting a 404 error. For example:
The requested URL /home/username/public_html/contact.html was not found on this server.
Ideally, it should redirect to /~username/contact.html.
Addition information
When I used "Options -MultiViews" line above the code it is giving the following error:
500 Internal Server Error: The server encountered an internal error or misconfiguration and was unable to complete your request.
Apache/2.2.15 (Red Hat) server is used here.
Why I am facing this problem? Is root is automatically getting changed from public_html/ folder?
EDIT:
Directory Structure:
Username
.gnome2 (there are empty folders inside it)
.mozilla (there are empty folders inside it)
public_html (I have put css, fonts, js, etc folders and .htaccess, contact.html, index.html, etc files here.)
.bash_logout
.bash_profile
.bashrc
Username folder is under universitynameuniverse folder (whose other folders I cannot see).
Try this code in your .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
That’s it! You can now link pages inside the HTML document without needing to add the extension of the page.
Try this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [QSA,L]
</IfModule>
## Results
# ~user/contact => ~user/contact.html (only if file exists)
It only works if the ~user/ directory actually exists on the filesystem.
If you need an external redirect instead, append an R flag to the RewriteRule directive.
The problem with your rewrite rule is that it's appending a .html suffix to the whole %{REQUEST_URI} variable which will probably result in a 404.
Update
Re-reading your question I noted that you want to map the ~user part to somewhere out of the apache webroot. In this case, try this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/~(.+)/(.+)$
RewriteCond /home/%1/public_html/%2.html -f
RewriteRule . /home/%1/public_html/%2.html [QSA,L]
</IfModule>
## Results
# ~user/contact => /home/user/public_html/contact.html (only if file exists)

Htaccess not getting 'GET' variables even with QSA flags

I can't seem to get my 'GET' variables passing through with my htaccess code. Here it is below:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L]
RewriteRule ^/(.*)/(.*)/(.*)$ v.php?shareid=$1&filename=$2 [QSA, L]
</IfModule>
I have v.php in the root, not in any folder and I am trying to achieve the following URL:
http://localhost/v/ubgvfmrsazwxoyp/Screen%20Shot%202015-11-05%20at%2020.51.45.png
where ubgvfmrsazwxoyp is the shareid and Screen%20Shot%202015-11-05%20at%2020.51.45.png the file name.
None are getting registered when page is loaded.
Note: I also want to keep .php extensions hidden.
Any ideas?
RewriteEngine On
RewriteBase /
RewriteRule ^v/([^/]+)/(.*)/?$ v.php?shareid=$1&filename=$2 [L]
You were previously getting v as the shareid and ubgvfmrsazwxoyp as the filename.
NOTE: The above code is specifically for an .htaccess file inside the server root directory. If the rewrite rules are going in the vhost config or the server config file, you'll need to use:
RewriteRule ^/v/([^/]+)/(.*)/?$ v.php?shareid=$1&filename=$2 [L]

Enable human readable URL's in .htaccess

Preface: Yes this question seems like duplicated, and I found related questions, but answers from there didnt help to me. :(
Hello, I want to add human readable URL's support for my PHP project. For now my URL quesry string looks like:
index.php?url=main/index
I would like to make it looks like:
index.php/main/index
I read following articles:
Stackoverflow
Cheatsheet
Stackoverflow
Stackoverflow
But when I do this:
var_dump($_GET['url']); // get empty array
, get empty array like no url parameter added.
My current .htaccess:
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php?url=$1 [NC]
Can somebody help me please? Thanks!
URL: http://domain.com/index.php/controller/action
Rewritten URL: http://domain.com/index.php?url=controller/action
.htaccess
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index.php/(.*)$ /index.php?url=$1 [L,QSA]
Explanation:
The .* in the pattern ^index.php/(.*)$ matches everything after index.php/ on the incoming URL. The parentheses helps to capture the part as variable $1, which is then added at the end of the substitution URL /index.php?url= + $1.
[L, QSA]:
L ignore other rewrite rules, if this fits.
QSA means query string append.
You have
index.php?url=main/index
Yuo want to rewrite (and then user redirect) to this
index.php/main/index
What about trying this?
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^url=([a-z]+)/([a-z]+)$
RewriteRule ^.*$ http://mydomain.site/index.php/%1/%2 [R=302,L]
R=301 or 302 depend on your need
On this example i assumed that on the original url there are only a-z chars.
Just to clarify (due the fact generally the process is inverse) on this way the users will be redirect, this rules does not convert links on your page.
In line
RewriteRule ^(.*)$ index.php?url=$1 [NC] the (.*) matches the part of the url up to '?' where the query begins.
To use the values passed in the query you need the QSA flag. Means Query String Append.
If Your URL : www.abcd.com/index.php/abcd/...
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Try the below code, Only considered for index.php you can replace it as per your requirement. Let me know if you need further assistance.
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine ON
RewriteBase /
RewriteRule ^(index\.php)/([0-9a-zA-Z\/_-]{1,99}) index.php?url=$2
What worked for me is:-
<?php var_dump($_GET); ?> this is the only thing I did on url http://localhost/baba.php/abcd . and original .htaccess file
DirectoryIndex baba.php
RewriteEngine ON
RewriteBase /
RewriteRule ^(baba\.php)*/([0-9a-zA-Z\/_-]{1,99}) baba.php?url=$2
For Magento 2.4 Magento will always drop any GET parameters from the URL in a htaccess rewrite. The only way I found to make it work was to create a rewrite module. The setup is here: https://magento.stackexchange.com/a/158811/109113

htaccess rewriterule to send a file to another file in the same directory

I have a directory that contains an index.php and an index.html file, both being published from a CMS. There's a specific IP address that will attempt to access index.html, but should instead be shown index.php in the same directory. All other traffic should act as normal.
I've been working with some variations of this code:
RewriteEngine On
RewriteCond %{REMOTE_HOST} ^123\.456\.789\.10
RewriteRule ^index\.(htm|html?)$ index.php [NC,R=301,L]
This does do the redirect, but of course it goes to the root of the site rather than staying in the same directory. It's somewhat unclear what the directory path will be in all cases, so I'd like to tell Apache to stay in the same directory it's in.
Is this possible?
Thanks,
Jonathan
If you want to have this rule for IP: 123.456.789.10 then you shouldn't have this IP with
negation like: RewriteCond %{REMOTE_HOST} !
Try this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /subdir/
RewriteCond %{REMOTE_HOST} ^123\.456\.789\.10$
RewriteRule ^index\.html?$ index.php [NC,R=302,L]