I am working with my class on a project with an htaccess file that doesn't seem to work. I have this:
SetEnvIf Request_URI ^ root=/myroot/
RewriteRule ^(assets|inc) - [L]
RewriteRule ^section[/]?$ %{ENV:root}section/index
RewriteRule ^section/function/(.*)$ %{ENV:root}index.php?type=section&action=function [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^section/(.*)$ %{ENV:root}index.php?type=section&action=view [L]
RewriteRule ^index\.php$ - [L]
In fact, there is three things I don't understand:
If I query an url as /section/function/, I should get /myroot/index.php?type=section&action=function but I get localhost/section/function in my browser, leading to an error. Are the url translated bidirectionnaly : myroot/index.php?type=section&action=function appearing in the browser as /section/function/. If I delete the last line , I get blank error page, Why so ?
Edit :
So conformally to what Olaf Dietsche said, I changed the rules into
SetEnvIf Request_URI ^ root=/myroot/
RewriteRule ^(assets|inc) - [L]
RewriteRule ^section[/]?$ %{ENV:root}section/index
RewriteRule ^section/function/(.*)$ %{ENV:root}?type=section&action=function [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^section/(.*)$ %{ENV:root}?type=section&action=view [L]
RewriteRule ^index\.php$ - [L]
But when I click in , I am shown a page localhost/section/some function. I really don't know what is going wrong....
First, you must have
RewriteEngine on
in the .htaccess for the rules to work.
The rules rewrite the requests, but don't redirect the client. The difference between the two is
rewrite send another page's content to the client, without telling the client. The client thinks, that it got the contents of the requested page.
redirect send another URL to the client, causing the client to make a new request with this URL. The client knows (and displays in the browser's bar), that it received the contents of another page.
If you want to redirect the client, you must add an R flag to the RewriteRule
RewriteRule ^section/function/(.*)$ %{ENV:root}index.php?type=section&action=function [R,L]
Update:
You must remove %{ENV:root} in front of the substitutions, because the URL is e.g. /index.php and not /myroot/index.php
RewriteRule ^section[/]?$ /section/index
RewriteRule ^section/function/(.*)$ /index.php?type=section&action=function [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^section/(.*)$ /index.php?type=section&action=view [L]
RewriteRule ^index\.php$ - [L]
Related
I've made www.site.com/asd to redirect to www.site.com/index.php?page=asd. However, I would like to add one more thing, where www.site.com/products/asd would redirect to www.site.com/index.php?page=products&item=asd
I can't seem to get it working, I have this as my base code:
RewriteEngine On
RewriteRule ^(admin|pictures)($|/) - [L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !=/pictures/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?page=$1 [L,QSA]
I've tried adding:
RewriteRule ^([^/]*)/([^/]*)$ /index.php?page=$1&item=$2 [L]
in various locations of the .htaccess file, which either results in:
500 Internal Server Error
or
0 CSS files load in .com/products/asd And no pictures load in .com/anyfile
I understand it's path related, but is this a good way to go about it? Can I solve it somehow in the .htaccess file, or would I need to add some check that if it's product page and a product is shown, include ../css?
The ^(.+)$ regular expression gives you a "500 Internal Server Error" because the redirection matches to the rule. So you gets the following error:
Request exceeded the limit of 10 internal redirects due to probable configuration error.
You can use:
RewriteRule ^products/(.+)$ index.php?page=products&item=$1 [L,QSA]
RewriteRule ^([^\.]+) index.php?page=$1 [L,QSA]
So, the following URLs will be redirected to:
/products/asd to index.php?page=products&item=asd
/asd to index.php?page=asc
Change the last two lines like this :
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?page=$1&item=$2 [L]
You should specify that in first rule a URI must come like /whatever/ or /whatever so the second will match either /whatever/whetever or /whatever/whatever/ and both will work individually without any interference.
I've two types of URLs:
http://www.example.com/?content=contact_form.php
and
http://www.example.com/?content=product.php&id=20
I changed my whole URL system like this:
http://www.example.com/file/contact_form
and
http://www.example.com/product/I-m-the-title/20
Of course I made 301 redirect with .htaccess to tell Google and co. the new URL.
I made it like this:
# Rewrite URLs
RewriteEngine On
RewriteRule ^(ignore)($|/) - [L]
RewriteRule ^file/([^/]*)$ /?content=$1.php [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /?content=$1.php&title=$2&id=$3 [L]
# Redirect old URL to new URL
RewriteCond %{QUERY_STRING} ^content=contact\_form\.php$
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule .* http://www.example.com/file/contact_form? [R=301,L]
RewriteCond %{QUERY_STRING} ^content=product\.php&class=I-m-the-title$
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule .* http://www.example.com/I-m-the-title/Test/20? [R=301,L]
My problem:
It's perfectly working for: http://www.example.com/?content=product.php&id=20
But for http://www.example.com/?content=contact_form.php I'm getting the message that it couldn't get opened because of too much redirect.
Does anybody know what I'm doing wrong? I hope anybody can help me soon because I have to fix it before Google misinterprets it.
Your rule cause an infinite loop because it is rewriting your uri to the same location again and again overriding your internal and external redirects.. To fix the Rewrite loop, add the following at the top of your htaccess
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
I have an htaccess rewrite URL as below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mywebsite.com$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^my-page\.html$ /my-page.php [L]
RewriteRule ^my-page/([^/]*)\.html$ /level1.php?num=$1 [L]
RewriteRule ^my-page/([^/]*)/([^/]*)\.html$ /level2.php?level1=$1&level2=$2 [L]
RewriteRule ^([^/]*)\.html$ /level3.php?level3=$1 [L]
These rules above rewrite URLs from mywebsite.com/my-page.php to mywebsite.com/my-page.html.
Now, what I want to achieve is mywebsite.com/my-page/ to be redirected to mywebsite.com/my-page.php (which in turn rewrites to mywebsite.com/my-page.html).
What I have tried, I created a directory "my-page" and tried to redirect requests from mywebsite.com/my-page/ to /my-page.html.
I don't know what went wrong. I can see in the network tab that a request is made to /my-page/ and gets rewritten to mywebsite.com/my-page.htmlmy-page/, which gives a 302 Status ☹
Please help! Thank you.
You can try use RedirectMatch to achieve this.
Redirect to my-page.php:
RedirectMatch 301 ^/my-page/ http://mywebsite.com/my-page.php
or straight away to my-page.html if this is your goal:
RedirectMatch 301 ^/my-page/ http://mywebsite.com/my-page.html
or, what will be best - change the code responsible for mywebsite.com/my-page.htmlmy-page/, but I can't see it in question you have asked :)
Please give the following a try. Brief descriptions are found in the comments for each section.
RewriteEngine On
# Trim www.
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [R=301,L]
RewriteBase /
# Redirect /my-page[/] to /my-page.html
# >> Note: change 302 to 301 to make permanent
RewriteRule ^my-page/?$ my-page.html [R=302,L]
# Allow existing files and directories
# Recommended to comment out the first line
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite *.html to respective page
RewriteRule ^my-page.html$ my-page.php [L]
RewriteRule ^my-page/([^/]*).html$ level1.php?num=$1 [L]
RewriteRule ^my-page/([^/]*)/([^/]*).html$ level2.php?level1=$1&level2=$2 [L]
RewriteRule ^([^/]*).html$ level3.php?level3=$1 [L]
The important part here is that you do the required redirect before any other rewrites (except the www. removal).
Also, you previously had the two conditions which stated that if the request was not for a file or directory, then proceed with the next rule, but that wouldn't have accounted for the last two rules. As such, this version tells Apache to stop everything if the request is for an existing file or directory. I would recommend, for security purposes, that you comment out the line that checks for existing directories.
Few days back I had posted a question related to this and I got some great push. Also I did some experimenting and research and achieved some good result. here is what exactly I have achieved. I put this in my .htaccess in my folder -'folder':
RewriteEngine On
#Look for the word "post" and take everything after "post" and store it in $1
RewriteRule post(.+)$ something.php?file=$1 [L]
# allow public pages
RewriteRule ^index.php$ - [L]
RewriteRule ^style.css$ - [L]
RewriteRule ^bg.png$ - [L]
RewriteRule ^cat_bgg.gif$ - [L]
RewriteRule ^cat_bgr.png$ - [L]
RewriteRule ^h1_bg.gif$ - [L]
RewriteRule ^hd_bg.png$ - [L]
RewriteRule ^menu_bg.png$ - [L]
# serve everyone from specific-domain or specific-user-agent
RewriteCond %{HTTP_REFERER} ^http?://www.abc.com [OR]
RewriteCond %{HTTP_USER_AGENT} ^specific-user-agent$
RewriteRule ^ - [L]
# everybody else receives a forbidden
RewriteRule ^ - [F]
ErrorDocument 403 /index.php
So this is how it is working now:
I put a link on:
www.abc.com
as
www.xyz.com/folder/post(somecode)
When someone clicks this link, as per .htaccess 'somecode' is taken and stored in $1 and processes:
www.xyz.com/folder/something.php?file=$1
and displays this content. But the url shown in browser is:
www.xyz.com/folder/post(somecode).
Now this is what I want to do. Instead of displaying:
www.xyz.com/folder/post(somecode)
can i show something nice like
www.xyz.com/somethingnice
or
www.xyz.com/folder/someword
How do I do this? Should I add something after the line
RewriteRule post(.+)$ something.php?file=$1 [L]
Or is there some other method?
My code:
RewriteEngine on
RewriteBase /
RewriteRule ^article/([a-z]+)/?$ index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ index.php?page=$1 [L]
I am using WAMP and had setup a Virtual Host.
In my index.php, there is code to get page passed and checks if it exists(in database). If not, display an error message. It works fine.
Eg: http://mysite/contactus/
But it will not work if I use a a directory name as page_name in the URL. Eg: http://mysite/images/. This will display page not found error (ie. checks database and no page found, so display "not found"). But it will not display images,css(linked file) in the page. Also, it shows http://mysite/images/?page=images in addressbar.
Like that, if I goto js folder which is used to store javascript files, above problem occurs. So, problem is caused if any subdirectory's name is passed as pagename.
How to solve this ?
When http://mysite/images/ is supplied, mod_rewrite is redirecting to http://mysite/images/index.php?page=images instead of http://mysite/index.php?page=images
Edit
Please tell me how to block hotlinking of files and directory, and redirect back to index page or send some browser header error ?
I tried this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*) http://%{HTTP_HOST} [R,L]
RewriteRule ^article/([a-z]+)/?$ /index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [L]
Edit
New code(semi-working):
RewriteEngine on
RewriteBase /
# remove trailing slash ONLY if it is not an existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1/ [R,L]
RewriteRule ^article/([a-z]+)/?$ http://%{HTTP_HOST}/index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ http://%{HTTP_HOST}/index.php?page=$1 [L]
This code will clear the problem with not displaying pics and css when a directory name is mentioned. But whatever pagename i specify eg:http://mysite/contactus, it will goto URL: http://mysite/index.php?page=contactus. Even if I use a directory name eg: http://mysite/js, it will goto: http://mysite/index.php?page=js
I am very confused.
RewriteEngine on
RewriteBase /
RewriteRule ^article/([a-z]+)/?$ index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/*$ /index.php?page=$1 [L]
you have to put the slash in front.
Edit: changed the ? to *
My understanding is that your script is for documents only, not images or other resources.
Then you should ignore them right away. Try adding this line right after RewriteBase like this :
RewriteEngine on
RewriteBase /rewrite/
RewriteRule ^/(images|js)/(.*)$ - [L]
RewriteRule ^article/([a-z]+)/?$ index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ index.php?page=$1 [L]
Then these subdirectories would be served right away, thus bypassing the next RewriteRule set.
For the problem with the directories I usually force a slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+[^/])$ $1/ [R]