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]
Related
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ category.php?slug=$1
RewriteRule ^([^/\.]+)/([^/\.]+)$ product-details.php?slug1=$1&slug=$2
RewriteRule ^([^/\.]+)/?$ infrastructure-details.php?slug=$1
what I have already tried
This is my htaccess file. problem is when I am trying to execute (infrastructure-details.php?slug=$1) its move to (category.php?slug=$1) conflict with first rule of htaccess.
I tired multiple rewrite methods but its not working. Please help to solve this issue.
localhost/project/category.php?slug=pump, localhost/project/infrastructure-details.php?slug=paint second url i want to be-> localhost/project/paint both page is different. can you please specify how to write rules for this different pages.
There is no discernible pattern that differentiates these two URLs so the only way to implement these two rewrites is to hardcode them. For example:
RewriteRule ^pump$ category.php?slug=$0 [L]
RewriteRule ^paint$ infrastructure-details.php?slug=$0 [L]
Where the $0 backreference in the substitution string contains the entire match by the RewriteRule pattern (just saves some repetition).
If you need a more general solution (as your directives suggest) then there needs to be a discernible pattern in the URL that differentiates URLs that should be rewritten to category.php and infrastructure-details.php respectively.
I'm assuming your .htaccess file, and other files, are is inside the /project subdirectory.
RewriteRule ^([^/\.]+)/?$ category.php?slug=$1
RewriteRule ^([^/\.]+)/([^/\.]+)$ product-details.php?slug1=$1&slug=$2
RewriteRule ^([^/\.]+)/?$ infrastructure-details.php?slug=$1
Rule #1 and #3 conflict - they use exactly the same pattern (regex) to match against the requested URL. The first rule is always going to "win" and rewrite the request before rule#3 is able to process the request, so rule#3 never matches.
To write a generic rule like this there needs to be a discernible difference between the URL types that you can match with a pattern/regex. For example:
/category/pump
/infrastructure/paint
And then you can construct rules...
Options -MultiViews
RewriteRule ^category/([^/]+)$ category.php?slug=$1 [L]
RewriteRule ^infrastructure/([^/]+)$ infrastructure-details.php?slug=$1 [L]
Note that the order of these directives can be important. More specific rules need to be before more generalised rules.
Options -MultiViews
RewriteEngine on
RewriteRule ^infrastructure/([^/]+)$ infrastructure-details.php?slug=$1 [L]
RewriteRule ^([^/\.]+)/?$ category.php?slug=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ product-details.php?slug1=$1&slug=$2 [L]
This is work fine for me. (infrastructure-details.php?slug=$1 [L]) put on top.
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
I'm trying to rewrite these two URLs
domain.com/test
domain.com/TEST
to this
domain.com/test.php
This is my current .htaccess code:
Options +FollowSymlinks -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(test|TEST)($|/$) /test.php
This works for the /test URL, but not for /TEST. What am I doing wrong?
Use of the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner. That is, it doesn't care whether letters appear as upper-case or lower-case in the matched URI.
You have to add [NC] as suffix in rewriteurl
Prashant Thakre comment about the [NC] flag is correct. The code below looks for /test while ignoring case and sets the permanent redirect flag.
RewriteEngine On
RewriteCond %{REQUEST_URI} /test [NC]
RewriteRule .* http://example.com/test.php [R=301,L]
Here is another option for permanent redirection but it is case sensitive so multiple lines of code are used to match lower and upper case.
RewriteEngine On
Redirect 301 /test http://example.com/test.php
Redirect 301 /TEST http://example.com/test.php
You can find more information about mod_rewrite options at http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Remember to always clear your browser cache when testing mod rewrites. When browsers cache pages, rewrites are usually ignored even if the rewrite works fine.
It is posibble do the same for all the pages and with capital and lower letters?
For example contact.html, services.html, prices.html. when you have the rewrite rules to remove .html extension, and when you write , for example, mydomain.com/contact.html or mydomain.com/contact both cases shows mydomain.com/contact and loads contact.html, but, what can you do to make it works when you write mydomain.com/ConTaCt.html or mydomain.com/COntaCT, and other possbilities for all the pages? Services, prices and all the others, working with or without caps or lower
I have a PHP site which replaces an ASP site, so the path structure is different.
In the URLs, I need to match http://apache.site/Cartv3/Details.asp & redirect to another location. What is the correct syntax to match that URL fragment?
I've already tried
RewriteCond %{REQUEST_URI} CartV3/results1.asp?Category=60
RewriteRule ^(.*)$ home-study/A-Levels/1/page-1 [R=301,L]
and
RewriteRule ^CartV3/Details\.asp?ProductID=1004 home-study/A-Levels/1/page-1 [R=301,L]
You meed to read more about mod_rewrite. Remember RewriteRule doesn't match query string. You attempt needs to be rewritten as:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^Category=60$ [NC]
RewriteRule ^CartV3/results1\.asp$ /home-study/A-Levels/1/page-1? [R=302,L,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
PS: ? after page-1 is a special mod_rewrite syntax to strip original query string. If you want to keep original query string in rewritten URL then take out ? in the end.
The problem here is that you are trying to match the query string, which has to be done by a separate RewriteCond. If you want the match specifically "Category=60", then you can add it as a Condition:
RewriteCond %{QUERY_STRING} Category=60
RewriteCond %{REQUEST_URI} /CartV3/results1.asp
RewriteRule .* home-study/A-Levels/1/page-1?
This will match http://example.com/CartV3/results1.asp?Category=60 and redirect. The ? at the end of the rule stops "?Category=60" being to the resulting URI.
If you don't care about the value in the query string, then you can remove the first condition.
I want all my URL with matching pattern as
/released/2013/iron-man
/released/2013/abc-of-death
/released/2012/saw6
to be redirected to
/released/1.php
and I can the name as well.
I am adding this rule to my .htaccess file but its not working
RewriteRule ^released/([0-9]+)/?$ /released/1.php?id=$1 [L]
The trailing question mark matches an optional ending / which is not what you want.
^released/([0-9]+)/iron-man$
or
RewriteRule ^released/([0-9]+)/(.+)$ /released/1.php?id=$1+$2
Problem is that you have $ after second slash but you have movie name after 2nd slash like iron-man etc. Remove $ since you are not matching it.
Make sure that mod_rewrite and .htaccess are enabled 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 /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(released)/([0-9]+)/ /$1/1.php?id=$2 [L,QSA,NC]
A RewriteRule which does not specify a specific external domain is always executed internally. To make it redirect as you ask add [R=301] at the end - or 302, 303 or 307 depending on which kind of redirect you require, but usually 301 is fine.
Besides that, the regular expression you wrote does not allow for extended URLs - remove the trailing $. After that the /? part is moot so you can remove it as well.
The resulting line would read:
RewriteRule ^released/([0-9]+) /released/1.php?id=$1 [L,R=301]