I have a directory structure as follows:
/gallery
----index.php
----/23XASDTAGH
----/24XGA43KJA/
I'd like to use mod rewrite to rewrite if a directory exists. so:
www.example.com/gallery/23XASDTAGH/
becomes
www.example.com/gallery/index.php?gallery=23XASDTAGH
but i'd like to do this silently, so no changes happen to the url. now i have this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}/ -d
RewriteRule (.*) /gallery/index.php?gallery=$1 [L]
which works with
www.example.com/gallery/23XASDTAGH/
but the odd thing is when i leave the trailing slash off the end it changes the url to
www.example.com/gallery/23XASDTAGH/?gallery=23XASDTAGH
how can i get it to work with or without the trailing slash?
I think the following will do the trick
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) /gallery/index.php?gallery=$1 [L]
Second line checks that the requested filename is a directory and if it is then the third line does the actual rewrite
To try and deal with the trailing slash problem, maybe this, or something like this will work
RewriteCond %{REQUEST_FILENAME} (.*)/?$
RewriteCond %1 -d
RewriteRule (.*)/$ /gallery/index.php?gallery=$1 [R=301]
You may want to try this:
RewriteEngine on
RewriteRule ^(gallery)/([a-zA-Z0-9-_=]+)/?$ http://www.example.com/$1/index.php?$1=$2 [R=301,L]
Permanent redirect the entered URL (The one in the browser address bar):
www.example.com/gallery/23XASDTAGH or www.example.com/gallery/23XASDTAGH/to:
www.example.com/gallery/index.php?gallery=23XASDTAGH
Finally figured it out, i needed a ?/ at the end of my expression in the rewrite rule to say that the slash was optional. Why do regular expressions have to be so hard easy. 8-)
correct expression:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([a-zA-Z0-9_-]+)?/$ /gallery/index.php?gallery=$1 [L]
Related
I currently use $_GET['base'] to determine which homepage that the user visits.
This results in localhost/?base=administrator or localhost/?base=guest
I am also using this to control which page is the user at, such as
localhost/?base=guest&page=register
Is there any way to use mod_rewrite, or htaccess, to change how this system works?
Modifying my code is not an issue, is this possible?
EDIT:
I am trying to achive this:
localhost/?base=guest to localhost/guest
localhost/?base=admin to localhost/admin
localhost/?base=guest&page=register to localhost/guest/register
Below is my htaccess file
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
Will the document path affect how it is being called? As I am using a case loop to include which items are needed.
This, however, works for localhost, but it will loop every other address to main.
RewriteEngine On
RewriteRule ^$ /index.php?base=guest[L]
But did not give a result as expected.
Your rules in .htaccess need to be in reverse order, like below:
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
That is because if it is kept in the order you have it, both localhost/?base=guest&page=register & localhost/?base=administrator will match the rule RewriteRule ^([^/]*)$ /?base=$1.
Having them in reverse order ensures that the first rule is matched only for localhost/?base=guest&page=register. It won't match the first rule for localhost/?base=administrator. I hope that helps.
You need to exclude your existent files and folders from the rule
RewriteEngine On
# if the request is a dir
RewriteCond %{REQUEST_FILENAME} -d [OR]
# or file
RewriteCond %{REQUEST_FILENAME} -f
#do nothing
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
So you can use this simple code:
RewriteEngine on
RewriteRule ^(\w+)$ index.php?base=$1 [L]
RewriteRule ^(\w+)/(\w+)$ index.php?base=$1&page=$2 [L]
\w will match symbols a-z, 0-9 and underscore _, I think those characters are enough for your case, but if you need expansion it will be easy
Also in this case you don't need to change your code, because you still get base and page parameters in the $_GET array
UPDATE:
to disable query string params page and base (other params may be needed) add these two lines to the code at the bottom:
RewriteCond %{THE_REQUEST} (\?|&)(page|base) [NC]
RewriteRule .* - [L,R=404]
I need some help to write a rewrite rule : I tested many many things but i guess i'm doing something wrong.
I need to rewrite this kind of url:
this is the FROM url :
http://website.com/a-section-a/a-section-b/a-section-c/99999-name-name2#
to:
this is the TO url :
http://website.com/index.php/newsection/99999-name-name2
I tried many thing but actually i get it:
RewriteRule /index.php/newsection/ \/([a-z]+([-]|[\/]))+
But not working ( rewrite engine ON ).
edit : The url should redirect to the TO page AND rewrite it.
A bit unclear on how your rewrite rule should work due to your syntax and the odd placement of /index.php/, but try using this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)/(.*)?$ /index.php/newsection/$4 [R,L,NC]
Just so you understand how it works, the RewriteEngine On simply tells Apache to turn on the rewrite rule engine in the ruleset. The RewriteCond %{REQUEST_FILENAME} !-f assures the rule only kicks in of there isn’t a file with the same name. Similarly, the RewriteCond %{REQUEST_FILENAME} !-d assures that the rule does not kick in if there isn’t a directory with the same name.
Now the actual RewriteRule breaks down like this:
Each ([a-z0-9-]+) represents a segment of the URL path. It only matches the letters a-z (case insensitive) & numbers 0-9 as well as the - character.
The / designates each path part like a real URL.
The last part of the path is (.*)?$ which will catch anything`.
The area past the regex stuff that matches the URL is the redirect destination with $4 matching the last thing captured by the regex stuff.
And the [R,L,NC] are Apache rewrite rule flags that equate to: R means redirect, L means last meaning the ruleset stops processing & NC means match the rule with “no case” (aka: case-insensitive).
try below code,
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.html HTTP/
RewriteRule ^index.html$ http://www.yoursite.com/ [R=301,L]
How to force remove trailing slash from specific URL using htaccess,
For example :
https://blahblah.com/checkout/?checkout=add_to_cart&id=17&item_options[price_id]=1/
to
https://blahblah.com/checkout/?checkout=add_to_cart&id=17&item_options[price_id]=1
and ignore other URL such as https://blahblah.com/about/ or https://blahblah.com/contact/ etc
Because the trailing slash is part of the querystring you can't just rewrite it, you have to extract the querystring minus that final / and then redirect to the page you're on with that match appended.
To do this you need to match on the pattern in the RewriteCond with %1 (see this answer for reference) and append that to the %{REQUEST_URI} (thus removing the original querystring) - like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)/$
RewriteRule ^.*$ %{REQUEST_URI}?%1 [L,R=301]
RewriteCond %{QUERY_STRING} ^(.*)/$ <-- make sure you've got that / at the end here in your conditional - it ensures that only querystrings that end with / are redirected so you don't end up in a horrible recursive loop.
You can try this:
RewriteCond %{QUERY_STRING} (.*)/$
RewriteRule ^(.*)$ /$1?%1 [L,R=301]
This will redirect every request containing query strings only. In your case this:
checkout/?checkout=add_to_cart&id=17&item_options[price_id]=1/
to
checkout/?checkout=add_to_cart&id=17&item_options[price_id]=1
But not this https://blahblah.com/about/ and https://blahblah.com/contact/
I would advise trying to find out why that trailing slash is there to begin with first. As CD001 said in the comments it could be that your PHP code is doing something wrong.
If however you do need a htaccess solution to remove the slash then the below should work for you.
RewriteCond %{QUERY_STRING} ^(.*)/$
RewriteRule ^(.*)$ ?%1 [L,R=301]
This has been tested at http://htaccess.madewithlove.be/ and works for your example URLs.
if you mean an internal routine
RewriteCond %{QUERY_STRING} ^(checkout\=[^\&\s]+\&id\=\d+\&item_options\[price_id\]\=\d+)\/
RewriteRule ^ %{REQUEST_FILENAME}?%1 [L]
I got stuck at this point here, and I'm breaking my head trying to figure out why its not working!
This is want to get the url to look like
example.com/news/top/
from
example.com/index.php?view=news&task=top
but i need it that way that "task" can be something diffrent like
example.com/index.php?view=news&task=newest
example.com/index.php?view=news&task=help
example.com/index.php?view=news&task=write
....
current .htaccess looks like:
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+) index.php?view=$1&task=$2
If I visit www.example.com/news/ -> it only loads the index.php but does not pass ?view=news
If I visit www.example.com/news/top/ -> it works just fine.
But I need them both to work, adding a new line to .htaccess like :
RewriteRule ^news/ index.php?view=news
then visiting www.example.com/news/ works fine, but visiting www.example.com/news/top/ it will not be possible to get the value of task.
Please help me!
Thank you in advance.
Try adding these rules to the htaccess file in your document root:
RewriteEngine On
# Redirect access to index.php
RewriteCond %{THE_REQUEST} \ /index\.php\?view=([^&]+)&task=([^&\ ]+)
RewriteRule ^ /%1/%2/? [L,R=301]
# rewrite to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?view=$1&task=$2 [L]
The first redirects requests like /index.php?view=news&task=foo to /news/foo/. Then the second rule internally rewrites requests like /news/foo/ back to /index.php?view=news&task=foo.
If all of your links already look like /news/foo/, then you won't need the first rule.
I'm no htaccess veteran, but something like this might work:
RewriteRule /news/(.*) /index.php?view=news&task=$1 [L]
This should rewrite all requests to /news/abc to index.php?task=news&view=abc. The backreference $1 stands for the first pattern in the RegEx that is surrounded by parentheses.
htaccess is enabled, i have the canonicalization running (no-www to www.)
I'm trying to use htaccess to do the following
www.domain.com/page.php?i=Page1
www.domain.com/page.php?i=Page2
To
www.domain.com/Page1
www.domain.com/Page2
I tried using this code snippet, with no luck so far:
rewriterule ^([a-zA-Z0-9_-]+)/$ page.php?i=$1
However I think I'm going the reverse way. I can't find example for this.
I have this but I can't make it work.
Try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/page\.php$
RewriteCond %{QUERY_STRING} ^i=([a-zA-Z0-9_-]+)$
RewriteRule ^(.*)$ http://www.domain.com/%1? [R=302,L]
taken from: http://www.simonecarletti.com/blog/2009/01/apache-query-string-redirects/
You want www.domain.com/Page1 to be visible in the browser's address bar, but internally that url should actually /page.php?i=Page1. In that case the problem is the trailing / in your regex:
Rewriterule ^([a-zA-Z0-9_-]+)/$ page.php?i=$1
^---here
Your desired /Page1 url has no trailing slash, yet your rewrite regex requires one, so the pattern doesn't match, and no rewriting occurs. Try removing the / and see if that helps any:
Rewriterule ^([a-zA-Z0-9_-]+)$ page.php?i=$1
^---no /
In order to parse out the query string you need to access it in a RewriteCond
# We need this line because /page.php?i=page.php will cause an infinite loop
RewriteCond %{QUERY_STRING} !i=page.php
# Now we parse out the value of i from the query string
RewriteCond %{QUERY_STRING} i=([^&]+)
RewriteRule ^page.php /%1 [L]