Enable human readable URL's in .htaccess - apache

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

Related

REQUEST_URI rewrite incl. query string

I have a WordPress plug-in and theme that are malforming a URL call to an external css file. I will fix that later, but until then, I need to do a redirect so the css is getting served correctly.
REQUEST_URI contains the full path, so why doesn't this work?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/?wp-content/plugins/jetpack/css/%22https:/fonts.googleapis.com/css?family=Open+Sans%22$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/?wp-content/plugins/jetpack/css/"https:/fonts.googleapis.com/css?family=Open+Sans"$
RewriteRule ^(.*)$ https://fonts.googleapis.com/css?family=Open+Sans [R,L]
</IfModule>
Note 1: I have tried several combinations of escaping characters in the RewriteCond. Nothing works.
Note 2: I am using flag [R,L] for testing, to prevent browser caching. Will change to [R=301,L] later.
Note 3: I have tested to verify that the problem is with the RewriteCond, not the RewriteRule.
UPDATE:
OK, so this was a bad question and a dumb mistake. Even though raw REQUEST_URI contains the query string, mod_rewrite specifically does not allow query string to be in REQUEST_URI for matching purposes. Hence, QUERY_STRING instead. I thought that it could be done either way.
I don't have a true query string in my REQUEST_URI -- I have the query string that is in a string that I wanted to match -- so that is why I didn't go to QUERY_STRING at the outset. But mod_rewrite still sees the ? as a typical query string start.
Sorry, guys. I can handle it like this until I fix the underlying problem in WordPress:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/?wp-content/plugins/jetpack/css/%22https:/fonts.googleapis.com/css(.*)$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/?wp-content/plugins/jetpack/css/"https:/fonts.googleapis.com/css(.*)$
RewriteRule ^(.*)$ https://fonts.googleapis.com/css?family=Open+Sans [R,L]
</IfModule>
With WordPress, always remember to put your rewrites above the WordPress rewrites.

Rewriterule : php page to another (with parameters) cause a 500 error

I have a simple question but can't find the answer.
I have a page: http://www.mysite.com/overview.php?lang=en&TM=15 and
I want to access a new page : http://www.mysite.com/aboutus/overview.php?lang=en&TM=15
.htaccess
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^overview\.php\?lang=fr&TM=([0-9]+)&TM=([0-9]+)*$ /aboutus/overview.php?lang=$1&TM=$2 [R=301, L]
Any idea to solve my problem ?
Thank you in advance
Dominique
Try this,
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/overview\.php$
RewriteRule ^(.*)$ /aboutus/$1 [QSA,L]
[QSA] option will Rewrite url along with query parameters, so no need to match separately for lang=en&TM=15 (if you want to pass same request parameters while rewriting url)

htaccess mod_rewrite RewriteRule

I am trying to set up an .htaccess file to convert an incoming link like:
http://domain.com/root/TopNav/SubNav/SEO-friendly-file-name-p#
into this:
http://domain.com/root/index.php?t=TopNav&s=SubNav&l=SEO-friendly-file-name&p=#
where p# is the page id and TopNav/SubNav represent the navigation menu path to the file
I have been able to get it to work in all cases except for when there are arguments after the .php (it does the mod rewrite, but loses the parameters). Originally, I was hoping to have the .htaccess parse the url string so that it was ready for the script to use, but at this point I would be happy with any solution that takes the incoming url and dumps it as a string onto root/index.php.
here's what I currently have in the .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)/(.*)\.php index.php?s=$1&p=$2 [L]
RewriteRule ^(.*)/ index.php?p=$1&s=$1 [L]
</IfModule>
ErrorDocument 404 /404.php
Any thoughts on what I might be doing wrong? Suggestions of a better way to get this done?
Thanks
** someone suggested changing the [L] to [L,QSA] and that seems to have worked. Thanks, whoever suggested that...
With a URU that looks like this: /TopNav/SubNav/SEO-friendly-file-name-p# you've got 4 groupings you need:
TopNav
SubNav
SEO-friendly-file-name
#
So you need to craft your regex so that it captures these 4 things in one go.
Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/(.+?)-p([0-9]+)$ index.php?t=$1&s=$2&l=$3&p=$4 [L,QSA]
This would go in the htaccess file in your /root directory

mod_rewrite with multiple query strings?

I'm trying to cleanup some URLs on my blog, so I've decided to look into mod_rewrite. I haven't a clue what I'm doing though, so I was hoping I could get some help :P I have links like http://kn3rdmeister.com/blog/post.php?y=2012&m=07&d=04&id=4. Although it works, and people still get the content I want them to have, I don't like them having to look at all the query strings. I want to turn the above link into http://kn3rdmeister.com/blog/2012/07/04/4.php.
This is what my .htaccess looks like right now.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^y=([0-9){4})&m=([0-9]{2})&d=([0-9]{2})&id=([0-9]*)$
RewriteRule ^/blog/post\.php$ http://kn3rdmeister.com/blog/%1/%2/%3/%4.php? [L]
Like I said, I'm absolutely clueless :D
If you're using apache 2.0 or higher, you're going to need to remove the leading slash (the prefix) if these rules are in an .htaccess file, so that your regular expression looks like this:
# also note this needs to be a "]"--v
RewriteCond %{QUERY_STRING} ^y=([0-9]{4})&m=([0-9]{2})&d=([0-9]{2})&id=([0-9]*)$
RewriteRule ^blog/post\.php$ http://kn3rdmeister.com/blog/%1/%2/%3/%4.php? [L]
This is going to make it so when someone puts http://kn3rdmeister.com/blog/post.php?y=2012&m=07&d=04&id=4 in their browser's URL address bar, their browser will get redirected to http://kn3rdmeister.com/blog/2012/07/04/4.php and the new URL will appear in their address bar.
I assume you've got something setup on your server to handle a request like blog/2012/07/04/4.php.
At first you should define your URLs!!!
Like:
/blog shows front page
/blog/1234 shows post 1234
/blog/date/2012 shows posts by year
/blog/date/2012/06 shows posts by year and month
/blog/date/2012/06/01 shows posts by year and month and day
and so on...
First option is to rewrite each of your defined URLs to index.php. Your index.php has only to handle the submitted GET parameters.
### Do only if rewrite is installed
<IfModule mod_rewrite.c>
### Start rewrite and set basedir
RewriteEngine on
RewriteBase /
### Rewrite only if no file link or dir exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
### Rewrite frontpage
RewriteRule ^blog$ /index.php?action=showfront [L,QSA]
### Rewrite post
RewriteRule ^blog/([0-9]+)$ /index.php?action=showpost_by_id&id=$1 [L,QSA]
### Rewrite posts by date
RewriteRule ^blog/date/([0-9]{4})$ /index.php?action=showposts_by_date&year=$1 [L,QSA]
RewriteRule ^blog/date/([0-9]{4})/([0-9]{2})$ /index.php?action=showposts_by_date&year=$1&month=$2 [L,QSA]
RewriteRule ^blog/date/([0-9]{4})/([0-9]{2})/([0-9]{2})$ /index.php?action=showposts_by_date&year=$1&month=$2&day=$3 [L,QSA]
### Rewrite posts by tag
RewriteRule ^blog/tag/([a-zA-Z0-9_-]+)$ /index.php?action=showposts_by_tag&tag=$1 [L,QSA]
</IfModule>
Test in index.php with:
print_r($_GET);
print_r($_POST);
The second option is to rewrite all URLs and your index.php needs to handle all possible URLs. So at first it needs something like a router that splits the incoming URL in parts and then send the requested page or an error-page. I would try this at first as the bloody school.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php%{REQUEST_URI} [L]
</IfModule>
Test in index.php with:
print_r(explode('/', ltrim($_SERVER['PATH_INFO'], '/')));
print_r($_GET);
print_r($_POST);
The third option is to use a PHP framework. A framework may help you to write your code quite fast. It delivers you many base-classes like a router. (f.e. ZendFramework, Flow3, Kohana, Symfony, CodeIgniter, CakePHP, yii and others). This will make you more advanced.
The fourth and laziest option is to use a ready made software like Wordpress.

direct all webpage requests with .index.html to /

I want to direct all requests for any URL that ends with index.html to /. I have one domain on the server.
Example:
If someone wants "www.thissite.com/index.html--it is directed to www.thissite.com/.
AND
if someone wants "www.thissite.com/anyword/index.html"--it is directed to www.thissite.com/.
AND
if someone wants "www.thissite.com/folderdoesntexistonthissite/index.html"--it is directed to www.thissite.com/.
What is the .htaccess code that would enable this? (Both the rewritecondition and rewriterule)
This doesn't quite do the job:
RewriteCond %{THE_REQUEST} index\.html [NC]
RewriteRule index\.html$ http://www.thissite.com/$1 [R=301.L]
You could try this (without RewriteCond):
RewriteRule /index\.html$ http://www.thissite.com/ [R=301,NC,L]
Maybe the Error was the Period in [R=301.L].
You will need to use %{REQUEST_URI} variable to match in RewriteCond otherwise Apache strips out starting / in RewriteRule. Use below code in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} ^.*/index.html$ [NC]
RewriteRule . / [R=301,L]