Dear folks,
Imagine a One Page Site
RewriteCond %{HTTP_HOST} ^1pagesite.org$
RewriteRule ^$ 1pagesite.php [L]
Currently, the home page does present the 1pagesite.php correctly.
However, when another file is requested say, test.php which is on the server indeed, that test.php is shown!, while I would like to point ALL and ANY trafic towards 1pagesite.php for the time being. How to make this happen sothat no other file or folder/file or anything after this domain is presented and everything is directed towards 1pagesite.php?
Thanks very much for hints and suggestions to solve this puzzle. Cheers, Sam
RewriteRule ^.*(html|php)$ 1pagesite.php [L]
Note: This redirects only request to .html and .php pages
Can you try this rule:
RewriteCond %{HTTP_HOST} ^1pagesite.org$ [NC]
RewriteCond %{REQUEST_URI} !1pagesite.php$
RewriteRule \.(php|html)$ /1pagesite.php [R=302,L]
to redirect only php and html pages to 1pagesite.php
Your redirection only match ^$ that is to say only the root page (empty string).
RewriteCond %{HTTP_HOST} ^1pagesite.org$
RewriteRule ^.*$ 1pagesite.php [L]
So every request will be redirected to 1pagesite.php page (images, css, ... as Sander said).
If you need to filter some content you can add a
RewriteCond %{REQUEST_FILENAME} !^.*\.(css|jpg)$
to enable .css and .jpg file from your / directory to be served. This can be customized for other subdirecories files etc...
Related
I am using pretty url for my project and it is working fine.
http://testurl.com/user/12345
I am using .htaccess for redirection.
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/user\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ user/%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/user\.php\?id=([0-9]+)&name=([^&\s]+)\s [NC]
RewriteRule ^ user/%1/%2? [R=301,L]
RewriteRule ^user/([0-9]+)/?$ user.php?id=$1 [L]
RewriteRule ^user/([0-9]+)/([^/]+)$ user.php?id=$1&name=$2 [L]
Today i found if i change the link in browser like this http://testurl.com/user.php then page is also loading i want to show error message (Alert) if someone directly trying to access
The best way is to add your error or redirect in the user.php without id query string.
But you also can add (after RewriteBase /):
RewriteCond %{THE_REQUEST} \s/user\.php\s [NC]
RewriteRule ^ - [F]
You might as well solve your problem through code reorganization. I did this in one of my projects, and it has worked well.
1. When you create a pretty URL, move the according file into another directory
So, in this case, you had the URL example.com/user.php?id=123 visible externally. Now, you want a pretty URL for it, e.g. example.com/user/123.
On file level, before you had
- user.php
I suggest you move that to another directory, where all scripts live which are accessed by pretty URL only:
- rewrites/
|- user.php
2. Create redirects for your old URL to your new URL, externally
The same as you did above.
RewriteCond %{THE_REQUEST} \s/user\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ user/%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/user\.php\?id=([0-9]+)&name=([^&\s]+)\s [NC]
RewriteRule ^ user/%1/%2? [R=301,L]
3. Rewrite the new, pretty URL to moved script, internally
The same as you did above, with difference that the directory name rewrites is added.
RewriteRule ^user/([0-9]+)/?$ rewrites/user.php?id=$1 [L]
RewriteRule ^user/([0-9]+)/([^/]+)$ rewrites/user.php?id=$1&name=$2 [L]
4. example.com/user.php now fails with a 404
Because /user.php does not exist anymore in the file system, it automatically fails with a 404 if called without params.
5. Benefits
This approach might sound like additional work for nothing, but these are the benefits making it worthwile in my opinion:
You do not need an additional .htaccess rule for error handling
You get better code organization
You get better overview of what is already accessible with pretty URL
I have an issue writing some regex to go inside my htaccess file.
Basically, my site has been setup so that index.php and all other site files are not in the root (public_html) directory but instead are in http://fitnessquiz.co.uk/fitnessquiz.co.uk/
Initially I tried the following in my public_html folder:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^fitnessquiz.co.uk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.fitnessquiz.co.uk$
RewriteCond %{REQUEST_URI} !fitnessquiz.co.uk/
RewriteRule (.*) /fitnessquiz.co.uk/$1 [L]
which correctly navigates to my homepage and displays the url correctly but then when I click any link I get a "no input file specified" message. So then I tried replacing with:
RewriteCond %{REQUEST_URI} !^/fitnessquiz.co.uk/
RewriteRule ^(.*)$ /fitnessquiz.co.uk/$1 [L,R=301]
After which the site works but every url looks like this:
http://fitnessquiz.co.uk/fitnessquiz.co.uk/someotherfolder/etc.php
I've tried various htaccess regex solutions listed elsewhere on here but none seem to work, how do I accomplish both of these things i.e. redirect to /fitnessquiz.co.uk for every url but hide the duplicate url name/folder. Im on a shared server so don't have permissions to change any server/apache settings directly.
According to this answer by nuked on a previous post you could try:
RewriteCond %{THE_REQUEST} ^GET\ /fitnessquiz.co.uk/
RewriteCond %{HTTP_HOST} ^(www\.)?fitnessquiz.co.uk$
RewriteRule ^fitnessquiz.co.uk/(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?fitnessquiz.co.uk$
RewriteRule !^fitnessquiz.co.uk/ fitnessquiz.co.uk%{REQUEST_URI} [L]
This set of rules worked for me on a very similar situation. I had employed the same cure, (re-direct if calling the folder and hide after re-writing it) but I never got the order right on my own. Thus I kept seeing the page not found errors too. Below is my attempt to explain the actions, for my own learning, hopefully others too.
RewriteCond %{THE_REQUEST} ^GET\ /fitnessquiz.co.uk/
Is asking the question, does THE_REQUEST contain the subfolder you need to hide?
RewriteCond %{HTTP_HOST} ^(www\.)?fitnessquiz.co.uk$
Checks if the request is for the correct host.
RewriteRule ^fitnessquiz.co.uk/(.*) /$1 [L,R=301]
Rewrite the URL as one without the subfolder and call the new link in the redirected browser. Note:
L : Last step. Stop processing other rules
R=301 : After re-writing, redirect the browser to the new URL.
When the page is redirected it has no subfolder so the first RewriteRule is skipped. And then
RewriteCond %{HTTP_HOST} ^(www\.)?fitnessquiz.co.uk$
checks if calling the right host. And then
RewriteRule !^fitnessquiz.co.uk/ fitnessquiz.co.uk%{REQUEST_URI} [L]
rewrites the url that has not the subfolder to use the correct subfolder without redirecting the page, and while hiding actual subfolder from the browser. Again note:
L : Last step. Stop processing other rules
I have the following in my htaccess file:
RewriteEngine On
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
From rewrite.php I redirect to the correct pages depending on the url. Problem is that it redirects all files including css and js. I tried including these files but I now realise that was dumb of me. Should I redirect when there is and appropriate extension in the url? If redirecting is the way to go what method would be best? header location or HTTP_redirect?
Or is this not a good idea performance or work involved wise? I could go for something like this but I know next to nothing about apache and would rather not work with it right now.
RewriteRule ^(.*).css$ /includes/compressor.php?i=$1.css [L]
I previously had the following in my htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
I decided to remove this because:
I would not be able to include the header and other common files in the rewrite.php file. I would also not be able to have a database call in the rewrite file that would determine the page to include and to reuse the data for the page contents.
Unwanted files would be reachable such as service used only by external app.
The compression should be done once, and not for every request. You can then exclude requests from the URL rewriting if the corresponding file exists:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
How about redirecting only if the requested file does not exist on the server?
You could use the following rewrite conditions to achieve this.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
So if the request is for a CSS/JS/HTML/Image file that already exists on the server then no rewriting takes place and the request for the file is processed. If the file does not exist, it will run your rewrite rule to rewrite.php
I've been struggling with my mod_rewrite for a while now and I thought I'd cracked it…
My CMS generates blog post URLs with post.php?s= and I've been trying to remove this part of the URL so that they're nice and user friendly. Here's my mod_rewrite:
<ifModule mod_rewrite.c>
DirectoryIndex index.php index.html
ErrorDocument 404 http://tempertemper.net/error.php
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^www.tempertemper\.net$
RewriteCond %{REQUEST_URI} !cron.php
RewriteRule ^(.*)$ http://tempertemper.net/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^s=(.+)$
RewriteRule post.php /%1? [R=301,L]
RewriteRule ^resources /archive? [R=301,L]
RewriteCond %{REQUEST_URI} !post.php
RewriteRule ^([a-zA-Z0-9_-]+)$ post.php?s=$1
</ifModule>
Unfortunately, It seems to be directing all pages that aren't found to the blank post.php page instead of the error.php page.
My site is here: http://tempertemper.net and here's a URL that doesn't exist http://tempertemper.net/this-url-does-not-exist
It should take you to http://tempertemper.net/error
Thanks for taking a look :)
Because you are redirecting all paths which fit the [a-zA-Z0-9_-]+ pattern, this will simply send to your post.php script the query string s=this-url-does-not-exist. So it's up to your PHP script post.php to check whether this-is-not-a-url exists or not, as Apache cannot possibly know what blog post ID values are valid and which are not.
As a side note, I'd recommend using /blog/2012-08-24-whatever as the path you give to visitors, rather than /2012-08-24-whatever. That way you can reserve paths for other functions, such as /images without having to write a new exception in your .htaccess file every time you need to create a new utility path.
I need help configuring my .htaccess file to handle redirects properly.
Here’s what I need to have happen. Stackoverflow's spam filter wouldn't allow me to post the full domain. So where I say "DOMAIN" you can substitue "domain.com". (I also needed to add and extra t to the http.)
Requests for the DOMAIN/page version of the file should be redirected to www.DOMAIN/page.
Requests for the 'friendly' versions of the URLS should be allowed. So a file that is really at www.DOMAIN/index.php?q=37 should be viewable by going to www.DOMAIN/latest-news
I have a big list of 301 redirects. We recently changed the site from an .asp based CMS to one written in PHP.
Example:
redirect 301 /overview.asp http://www.DOMAIN/overview
Items 1 and 2 are working fine.
However for item 3, if I put in a browser request for "http://www.DOMAIN/overview.asp" instead of redirecting to the friendly name of the file ("http://www.DOMAIN/overview") it will redirect to http://www.DOMAIN/index.php?q=overview.asp. This is the problem.
What do I need to change to get this working right?
My configuration is below:
## Fix Apache internal dummy connections from breaking [(site_url)] cache
RewriteCond %{HTTP_USER_AGENT} ^.*internal\ dummy\ connection.*$ [NC]
RewriteRule .* - [F,L]
## Exclude /assets and /manager directories and images from rewrite rules
RewriteRule ^(manager|assets)/*$ - [L]
RewriteRule \.(jpg|jpeg|png|gif|ico)$ - [L]
## For Friendly URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.DOMAIN/$1 [R=301,L]
redirect 301 /overview.asp http://www.DOMAIN/overview
redirect 301 /news.asp http://www.DOMAIN/news
# ETC....
thanks!
Mod_rewrite is doing exactly what you're asking it to do ... (yes :-), that's often the problem with computers).
On the /overview.asp http://www.DOMAIN/overview line you're setting the browser to send out a brand new request from scratch, which starts the whole cycle again from the top and gets catched by the ^(.*)$ index.php?q=$1 directive.
Right before this line you should put another RewriteCond to prevent the ^(.*)$ rule to apply if REQUEST_FILENAME is either overview or news. You might also simply rewrite /overview.asp to overview [L] instead of redirecting.
If you can, set the RewriteLog directive to its highest verbosity and look at the logfile - it usually gives very good insights into what's really going on...
EDIT - if I get it right you shoud be doing this:
RewriteCond %{REQUEST_FILENAME} ! \.asp$
RewriteCond %{REQUEST_FILENAME} ! ^overview$
RewriteCond %{REQUEST_FILENAME} ! ^news$
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
This would prevent any file already ending in .asp, plus those looking for overview and news, to be redirected toward index.php.
I suspect anyway that you got something backwards regarding that SEO stuff. You should indeed start from the structure of the query string that your scripts expect and use that as a base to build a sensible URL addressing schema.
EDIT #2:
There was a space too many between the bang mark ant the regex. The following code doesn't come from memory as the previous - I've tested on my local Apache and it does what's supposed to do (as long as I've understood correctly..)
RewriteCond %{REQUEST_FILENAME} !\.asp$
RewriteCond %{REQUEST_FILENAME} !overview$
RewriteCond %{REQUEST_FILENAME} !news$
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Hope this helps