Seo-Friendly url's with .htaccess - apache

I already found several topics and questions regarding this, but it seems everything I'm trying to do doesn't do what it is supposed to do:
.htaccess-file
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?site=$1
As you can tell, nothing to special. This example was taken from this solution here: SEO Friendly URL (with .htaccess)
From my example:
http://example.org/index.php?site=delivery
should result in
http://example.org/Delivery # If it is possible, a capitalized Letter would be cool
I can call the Site manually, but references (to *.css-files) aren't included anymore. Furthermore, the RewriteBase doesn't seem to work.
I added some buggy code to verify that the .htaccess file is loaded (and it is).
If I linke something with <a href="index.php?site=mySite"> - does the URL automagically change to the specific rule? Yes, right? But what could be missing, that the rules aren't applying?
Thank you in advance

Your rule is fine. Because of the rewrite, you have to handle relative links differently now.
So once the rule is in place. You need to add this to the <head> section of your html.
<base href="http://www.yoursite.com" />
Then your JS and CSS will load while using the rule. It's not a .htaccess rule problem.
If I linke something with - does the
URL automagically change to the specific rule?
No it does not. You need a rule for that too.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond {THE_REQUEST} ^[A-Z]{3,}\ /+index\.php\?site=(.+)
RewriteRule ^ %1? [R=301,L]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?site=$1

Related

Apache htaccess rewrite (Pretty URLs)

I have a sneaking suspicion this is not possible, but figured I would ask regardless.
Is it at all possible to take a URL passed to a server in the form of:
http://domain.com/index.php?Action=Controller/Action&one=1&two=2&three=3
And rewrite it to appear as:
http://domain.com/Controller/Action/1/2/3
I am trying to clean up an borderline ancient project to support "Pretty URLs" and I would really like to make the URLs display a bit nicer. I know I could setup a 301 header redirect to the new URL, but I would prefer to avoid that overhead if at all possible.
Any thoughts?
Thanks!
To get
http://domain.com/index.php?Action=Controller/Action&one=1&two=2&three=3
To appear as
http://domain.com/Controller/Action/1/2/3
You will need to use %{QUERY_STRING} to capture the query string data. Your .htaccess file will look like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^Action=Controller/Action&one=(\d+)&two=(\d+)&three=(\d+)
RewriteRule ^.+ /Controller/Action/%1/%2/%3 [R=301,L]
This will set up a permanent redirect to the new page. You can play around and test .htaccess rewrite rules here: htaccess.madewithlove.be
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?Action=$1/$2&one=$3&two=$4&three=$5 [L,QSA]

Can I write an HTML response from an .htaccess file

Apologies if this is a stupid question, but can I control the HTML response from a .htaccess file (Apache)?
In other words something like (psuedo code) Write <!DOCTYPE html><html>...[etc]
The reason I ask is because I would like to "take down" some sites in one "hit", but without replacing any files or having any other kind of holding page.
I found the answer myself, certainly worked for what I needed:
ErrorDocument 503 "<!DOCTYPE html><html><head><title>This website is undergoing maintenance</title></head><body style='font-family: sans-serif'><h1>This website is undergoing maintenance</h1></body></html>"
RewriteEngine On
RewriteRule .* - [R=503,L]
Hope this helps somebody
No its not possible to produce HTML content into a rewritten URI. However what you can do is to have a HTML file pre-written let's call it outage.html which will be placed in your DOCUMENT_ROOT.
Then enable mod_rewrite and .htaccess through httpd.conf and place this code on top of your .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
#RewriteRule (?!^outage\.html$)^.*$ /outage.html [L,NC]
Whenever you want to bring the site down just uncomment above RewriteRule line by removing # and your site will just show outage.html to visitors for every URL.

Apache - rewrite images to php file with .htaccess

I'm looking for a way to rewrite all my image requests from one folder into some.php file, while preserving the original image url (or partial path).
So,
example.com/folder/img/test.jpg
would be rewrited as something like
example.com/folder/some.php?img=img/test.jpg
(is this the best approach?)
I'm not familiarized enought witrh regular expressions, so I'll be very thankfull :)
note : I've tried some solutions before, none of them worked. ALso, I'm running Apache 2.0 under CentOS environment.
Enable mod_rewrite and .htaccess 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 /
RewriteRule ^(folder)/(img/[^.]+\.jpg)$ $1/some.php?img=$2 [L,QSA,NC]
Make sure:
.htaccess is enabled
mod_rewrite is enabled
Your URL is http://example.com/folder/img/test.jpg
It sounds like you you want the filename of the image in the url to be included in the new php url, not the entire url. So something like:
RewriteRule ^folder/img/(.*[.]jpg)$ /folder/some.php?filename=$1
Considering what you mention in the comments and that the previous rules didn't work, I edited the message, this is what i have now.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)\.jpg [NC]
RewriteRule ^folder/img/([\w]*\.jpg)$ folder/some.php?img=img/$1[R=301,L]
If folder is al variable, you can change that for (\w*) and add the reference in the right side of the rule.
Hope this helps.
Bye

Using .htaccess Mod_rewrite to hide .php extension only working for missing pages

Please excuse me as I'm pretty new to using .htaccess, and am having a few issues with it as the setup I need is, despite being simple, seemingly rare.
Basically, I'm working on a secondary domain trying to hide .php extensions from pages, but whilst also redirecting requests for nonexistent pages to a custom search page. The overall desired effect is:
realpage => realpage.php
and
falsepage => search.php?q=falsepage
So far, the code I have (see below) seems to apply the latter correctly, but rather than returning 'realpage.php' for 'domain.com/realpage' it returns a 404 error.
I've found variations which also move realpage to 'search.php?q=missing' in case that gives any indication what might be going wrong!
My .htaccess file at the moment:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^([^\.]+)$ $1.php [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)\.php$ search.php?q=$1 [L]
Thanks for any help you can offer! It's very much appreciated!
I think for your first rule, you want something along the lines of:
RewriteBase /
RewriteRule ^([^.]+)$ $1.php [L]
I ran that through the rewrite rule tester, and it will direct 'realpage' to 'realpage.php'. If that doesn't work, I would suggest disabling your second rule, making sure the first one works well, then adding the check for missing files in later.

Mod-Rewrite Problems (Apache) with / slashes

I am betting on an obvious problem here I am not seeing.
Here's the important bits for those of you familiar with Mod-Rewrite
.htaccess file with mod-rewrite rules exists here:
http://www.thedomain.com/.htaccess
User goes to this URL:
http://www.thedomain.com/test/blog
Mod-Rewrite rules should actually tell the server to access this URL:
http://www.thedomain.com/index.php?page=blog
.htaccess:
Options FollowSymLinks
Options -MultiViews
RewriteEngine on
RewriteRule ^test/([^/.]+)$ /index.php?page=$1 [L]
This combination of code/request does not work. If you're wondering about the code snippet ^test not being ^/test instead, it is because apparently this is a problem on GoDaddy, the code fails with the / after the ^ - this seems like it may be related to my problem, which I'll explain further... If I change the .htaccess code line:
RewriteRule ^test/([^/.]+)$ /index.php?page=$1 [L]
to
RewriteRule ^test([^/.]+)$ /index.php?page=$1 [L]
(just removing the / here: ^test/([^/.]+) )
The code works when the requested URL is changed to accomodate (remove the slash; http://www.thedomain.com/testblog) as the user views the proper index.php?page=blog server response. It seems to me I cannot use any slashes within the darn match side of the RewriteRule. What gives?
Update: If at all relevent, this .htaccess file and the relevant files to the question all exist in a subdirectory off of the GoDaddy server that is hosting this although the domain points to the subdirectory as the root. Not sure if this is relevant.
Update: This server (at the server root) is actually running wordpress with pretty URLs enabled and they work perfectly fine. I assume wordpress uses mod-rewrite to make crazy urls like thedomain.com/2008/11/15/the-article-title.html work...?
Thanks so much.
Is RewriteBase what you're looking for?
there is a nice test utility for windows here
http://www.helicontech.com/download-isapi_rewrite.htm
try changing your code to:
^/test/([^/]+)$ /index.php?page=$1 [L]
or without slashes
^test[^a-z]+([a-z]*)$ /index.php?page=$1 [L]
I was unable to find a solid method around this problem on GoDaddy; for whatever reason I could not have slashes within the URL that was attempting to be rewritten aside from the base (http://www.somedomain.com/testingthis would work but http://www.somedomain.com/testing/this died).
I ended up instead using the Wordpress .htaccess to send all non-existant file/directory requests back to my index.php. I then used the $_SERVER['REQUEST_URI'] var with pathinfo() to parse the URL and then direct what content to load from the parsing. This works well, is fast, and is probably the same method Wordpress uses.
Thanks for the attemps!
If you're wondering about the code snippet ^test not being ^/test instead, it is because apparently this is a problem on GoDaddy, the code fails with the / after the ^ […]
That’s not odd but necessary:
Per-directory Rewrites
When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done.
And that per-directory prefix is for a .htaccess file in the document root (/.htaccess) the URL path root (/). Thus patterns with the ^ must be written without that per-directory prefix /.
On the same way the substitution is handled. After a rule is applied, the per-directory prefix is added to the substituion. So try this rule:
RewriteRule ^test/([^/.]+)$ index.php?page=$1 [L]
OK, first off, I think that the GoDaddy apache server simply has some of the options turned off. I think that if they don't have an AllowOverride FileInfo in their configuration, RewriteRule won't work so well, or at all.
Which means its surprising that the URL http://www.thedomain.com/testblog works at all, and gets re-written. So I guess I'm a little confused.
Here's an idea: Try creating a directory named test, and put the .htaccess file in there! It would look like this:
Options FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]
OK, another idea: Use RewriteCond. Maybe you can check the request URI directly, like this:
Options FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/test/([^/]+)
RewriteRule . /index.php?page=%1 [L]
Last idea: maybe your browser sees the URL http://www.thedomain.com/test/blog and thinks it's a directory, and adds a slash? So the URL is sends is http://www.thedomain.com/test/blog/. In that case, the REGEX won't match unless you allow for a trailing slash:
RewriteRule ^test/([^/.]+)/?$ /index.php?page=$1 [L]
Whoops. Sorry for gushing - there's just some many things that can go wrong in an HTTP request that goes through rewriting, and as many ways to try and overcome the problems :-)