.htaccess RewriteRule domain.com/page/page to domain.com?p=page/page - apache

I'm trying to set up my .htaccess file. How would I go about getting this result:
Request: domain.com/page/page
Rewrite to: domain.com/index.php?p=page/page
I've tried:
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]
with no luck.

Okay I worked it out. I needed two rules:
RewriteRule ^([a-zA-Z0-9_\\s-]+)(/)?$ index.php?p=$1
RewriteRule ^([a-zA-Z0-9_\\s-]+)/([a-zA-Z0-9_\\s-]+)(/)?$ index.php?p=$1/$2
My php script didn't like it because with just
RewriteRule ^([a-zA-Z0-9_\\s-]+)(/)?$ index.php?p=$1
domain.com/no1/ would translate into domain.com/index.php?p=no1/ <--- didn't understand the end slash
Only problem with this solution is it only goes two virtual dirs deep but as that's all I need it's fine.
This is my first time using .htaccess so I'm sorry for my lack of understanding.

Try this code in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteRule ^(?!index\.php)(.*)$ /index.php?p=$1 [L,NC,QSA]
Negative lookahead will avoid infinite looping here.

Related

Apache RewriteRule to hide Folder

I've read a lot of beginner tutorials and other threads here on the topic mod_rewrite, but I can't get my (simple) RewriteRule to work.
My local domain is http://localhost/movies, and my structure is:
/movies
|-.htaccess
|-index.php
|-/pages
|-profile.php
|-faq.php
|-statistics.php
So my URLs would be e.g.:
http://localhost/movies/pages/profile.php
But I want them this way:
http://localhost/movies/profile
I achieved this with simple rules in my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule profile pages/profile.php
RewriteRule faq pages/faq.php
RewriteRule statistics pages/statistics.php
</IfModule>
Now I've tried to sum it up in one rule:
RewriteRule ^(\w+)$ pages/$1.php
So my thought is, that /(\w+)$ captures any characters and puts it in $1
But this doesn't seem to work, so where might be my error?
The output I get: Object not found! 404
Edit:
The answers in the possible duplicate aren't working neither
RewriteRule ^/(.*)$ /pages/$1
Or
RewriteRule (.*) /subfoldername/$1

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

Using .HTACCESS Rewrite Rule

I'm trying to rewrite URLs for my dynamically generated PHP site.
I load new templates into index.php by using the following GET:
localhost/dmk/?req=signin
localhost/dmk/?req=useraccount
I want these links to appear as:
localhost/dmk/signin
localhost/dmk/useraccount
But for the life of me I cannot figure out how to do this. Everything I try either produces a 500 Internal Server Error, or has no effect at all.
I must be missing the point of RewriteRule.
You should read some documentation in this direction. I know it's a bit frustrating at first to write the rules, but it gets easier. You need to learn regular expressions to write the rules (you can start here: http://www.regular-expressions.info/)
As for the rules you need, they go like this:
RewriteEngine On
RewriteRule ^signin$ index.php?req=signin [L,QSA]
RewriteRule ^useraccount$ index.php?req=useraccount [L,QSA]
or
RewriteRule ^(signin|useraccount)$ index.php?res=$1 [L,QSA]
You can paste the rules you have used, maybe someone will explain you what you did wrong.
Try this
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^dmk/(.+)$ dmk/?req=$1 [NC,QSA,L]
This would redirect any URL like /dmk/page that does not conflict with an existing file or directory to /dmk/?req=page. I'm assuming your index.php is in /dmk directory.

mod_rewrite not working correctly locally

I have the following lines in my .htaccess:
RewriteEngine On
RewriteBase /new
Options +FollowSymlinks
Options -Indexes
#Shopping Redirects
RewriteRule ^shopping-bag?$ cart.php
RewriteRule ^checkout?$ checkout.php
RewriteRule ^product/([^/\.]+)/?$ product.php?fine_leather=$1 [NC,QSA,L]
Now, all of this works great on the production server, (last line will appear as http://www.domain.com/product/this-is-a-product) but locally, the first two lines work fine but the last doesn't. It doesn't seem to passing the GET variable :(
Any ideas/suggestions to fix this? I need to get the pretty URL's fixed but don't really want to work on the server until I know that they are working correctly.
Try this code :
RewriteRule ^shopping-bag?$ cart.php [L]
RewriteRule ^checkout?$ checkout.php [L]
RewriteRule ^product/(.+)$ product.php?fine_leather=$1 [NC,QSA,L]

htaccess is not working in two almost identical cases

This rewriting rule is not working (the id is empty after redirecting):
RewriteRule ^album/([0-9]+)$ album.php?id=$1 [L,QSA]
while this one does:
RewriteRule ^album([0-9]+)$ album.php?id=$1 [L,QSA]
The removed slash is the only difference.
This has to do with multiviews. Multiviews bypasses you rewrite rules, because it matched /album/... to the existing file album.php
You can prevent this by adding Options -MultiViews to your htaccess.
This has to do with the way mod_rewrite rules in .htaccess files are processed. Basically, the input to the RewriteRule will only be the "file" portion of the path, so when the URL is .../album/42, the RewriteRule will be matching against 42.
The solution is to use a RewriteCondition as well. Something like:
RewriteCond %{REQUEST_URI} (.*)/album/([0-9]+)$
RewriteRule ^ %1/album.php?id=%2 [L,QSA]