htaccess URL rewriting root url - apache

RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^$ http://example.com/index.php?flash=1 [L,R=301]
I have a page where I want to rewrite my base url, but I have some problems. I have the above in my .htaccess file but still the Rewrite will not take effect or work and Rewrite to index.php?flash=1 when users land on my page, what could the problem be?
In short what I want to do is that when someone lands on my page example.com/ the url should be example.com/index.php?flash=1
short: i want to change my root url when someone visits my page to something else, how?
This also has to do with mod_rewrite and the above question, is it searchengine friendly? if not how can i make it ? from what i heard with the url i use above searchengines with not index anything after the questionmark, what would be your suggestion to change if i want searchengines to index my pages ?

It sounds like you can use just a standard re-direct instead of dealing with re-writes.
place an index.html file in your root with this line in it:
<meta http-equiv="REFRESH" content="0;url=index.php?flash=1">
The index.html file should be viewed before index.php would be, then it just does a redirect.
If not place <meta http-equiv="REFRESH" content="0;url=index2.php?flash=1"> at the top of index.php and just make a new index2.php file to display the content you want.
It's a work around, but it's a quick fix.

Related

Loading site's 404 page instead of redirecting to it based on URL starting part

i'm trying to figure out how to load a chosen page (a 404 page) on the condition that the URL starts with img/galery/. We changed our image repositiory when we changed an eshop backend solution and a lot of crawlers are still trying to index our old images. Like an year+ old URLs. There's no way we could map old img URLs onto the new ones so i want to force a 404 there, because there are some htaccess rules that make those URLs a 301 and the crawler may think those images still exists, but they don't.
So basicly what i want to do is for any URL that starts with img/galery/ to load our site's /404/ page with no redirect. I just want to skip any redirect or any URL manipulation whatsoever so the crawers are given clear answer. I've tried hard, but as usual htaccess is really just black magic to me:
RewriteEngine On
# does not load the site's 404 page, just shows a plain (apache?) 404 page, but reacts on any URL starting with img/galery/
RewriteRule ^img/galery/ 404/ [R=404,NC,L]
# redirects to 404 page, but that's not what i wanna do
RewriteRule ^img/galery/ 404/ [R=301,NC,L]
# also redirects
RewriteRule ^img/galery/ 404/ [R,NC,L]
# does nothing
RewriteRule ^img/galery/ 404/ [NC,L]
# does nothing
RewriteCond %{REQUEST_URI} ^img/galery [NC]
RewriteRule ^.*$ 404/ [L]
I've tried many solutions from SO or other sites, none is working. What am i missing here? Thx!
It seems it's not possible to use RewriteRule to load dynamic 404/ page, so i made new 404.php file that imitates its mechanics.
I haven't found any other more direct solution than this, but maybe it will help somebody with the same struggle i had.

Rewrite rules for pages that do not exist in the database

As of now, my htaccess looks like this
RewriteEngine On
RewriteCond %{QUERY_STRING} ArticleID=([^&]+)
RewriteRule ^article.cfm$ /articles.php?id=%1 [R=301,L]
RewriteRule ^([a-zA-Z0-9-]+)$ page.php?slug=$1
RewriteRule ^([a-zA-Z0-9-]+)/$ page.php?slug=$1
ErrorDocument 404 "<H1>Page not found</H1>"
The first 3 lines allow me to redirect users who are access articles through .cfm to .php instead
For the 4th and 5th lines, my website allows creation of pages through a WYSIWYG editor and allow users to access it. The actual url will look like www.website.com/page.php?slug=homepage but after the rewrite rule, it will look like www.website.com/homepage
However, I was having problems where my admin pages such as www.website.com/admin/ was regarded as one of the page so I added the below codes and it works
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
But I'm still having a couple of problems.
If you access an invalid page such as www.website.com/hello/ when there is no such page created by the users at all, it will still show page.php but with empty contents, how do i redirect it to a 404 page?
How do I secure directories which does not have an index.php page? such as my images folder, includes folder, javascript folders etc?
Does my entire htaccess looks right at this current point?
Thank you guys so much for helping! I'm not good with mod_rewrite so I will appreciate your help!
If you access an invalid page such as www.website.com/hello/ when
there is no such page created by the users at all, it will still show
page.php but with empty contents, how do i redirect it to a 404 page?
You should do this with PHP. If there's no content included, use header() to redirect to a 404.
How do I secure directories which does not have an index.php page?
such as my images folder, includes folder, javascript folders etc?
Add this to your .htaccess: Options -Indexes
Does my entire htaccess looks right at this current point?
Looks fine.

htaccess on Localhost refers to root, not folder

I am working on a website to make the URL's more SEO friendly with the htaccess, however, i bump into a problem when I set up the htaccess file. If I use the URLs I prefer to setup, i get refered back to the localhost and not towards the development map I am working in, so in this case, i get refered back to localhost/blog for example instead of localhost/jellyfish_rework/blog, so I end up with a view from my localhostor simply an error in the page.
The original link was http://localhost/jellyfish_rework/index.php?p=blog
Options +FollowSymLinks
RewriteEngine On
RewriteBase /jellyfish_rework/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /?p=$1 [L]]
Does this have to do with the fact my anchors refer to <a href"/blog"></a>? Cause in the other anchor tags that I still refer to in the old way, I dont get the responds back I want (so the link stays http://localhost/jellyfish_rework/index.php?p=blog)
edit
the htaccess file is located in the localhost/jellyfish_rework/ as that is where the index file is.
Your last line (RewriteRule) should look like this
RewriteRule ^([^/]*)$ index.php?p=$1 [L]
Don't add a leading slash before.
Otherwise it will be like an absolute path (/index.php from root).
With the rule above and your RewriteBase, this rule will act this way:
If not existing file/folder then rewrite it to /jellyfish_rework/index.php?p=URI
Your old rule (with leading slash) was about:
If not existing file/folder then rewrite it to /index.php?p=URI
Note: since you're using relative paths, you must also change all your html links (css, javascript, images, links) with a leading /jellyfish_rework/ or use the base tag
<base href="/jellyfish_rework/">

Clean URL not working properly

Hey I'm struggling with this all day long and I'm new to clean URLs.
My Domain which i want to change:
http://www.domain.tld/ausflug.php?lang=de
to:
http://www.domain.tld/ausflug/lang/de/
via .htaccess. This is my code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ausflug/lang/(.*)/ ausflug.php?lang=$1
My Problems are:
If I navigate through my page, the url still is the ugly one.
And if I want to reach the page with the clean url, it won't display all the
css and js stuff.
Thanks for help!
First of all, replace your current code by this one (your htaccess should be in root folder)
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/ausflug\.php\?lang=([a-z]{2}) [NC]
RewriteRule . /ausflug/lang/%1/? [R=301,L]
RewriteRule ^ausflug/lang/([a-z]{2})/$ /ausflug.php?lang=$1 [L]
This will redirect domain.tld/ausflug.php?lang=de to domain.tld/ausflug/lang/de/ and will internally rewrite it (without any redirection loop error) to domain.tld/ausflug.php?lang=de.
For your second problem:
if I want to reach the page with the clean url, it won't display all
the css and js stuff
Simply use absolute paths for your links (with a leading slash).
Example: /css/file.css instead of css/file.css (otherwise it will look in wrong directory, since your rule is generating some virtual directories)
Or you can add this html tag <base href="/"> right after <head> in all pages (instead of replacing each links)

.htaccess redirect doesn't hide url

my .htaccess in the root folder includes the following lines :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ http://example.com/?city=$1 [NC]
when I open the address http://example.com/bla.htm, my browser doesn't hide the GET values specified in the .htaccess, it redirects me to ?city=bla. eventhough I'm not using the [R] switch. This has always worked for me before (as I remember, haven't dealt with htaccess in a while). What's wrong here ?
When you redirect to an entire URL, it doesn't do URL rewriting (you can't exactly rewrite URLs on someone else's website).
Assuming both URLs are on the same server, you need to do something like
RewriteRule ^(.*)\.htm$ index.php?city=$1 [NC]
Also, I'd recommend getting into the habit of using the [L] switch whenever you can - it helps avoid bugs when you have a lot of URLs to rewrite.