get the index.php with .htaccess - .htaccess

I'm using a bunch of ReWrite rules in my .htaccess such as these
RewriteRule ^brochure/([^/]+)/?$ brochure.php?cat_path=$1
It's all working great thanks to answers I found by searching this site. But I've got a tiny little snag.
This rule works perfecty at the moment
RewriteRule ^([a-zA-Z0-9_-]+)?$ $1.php It catches all my urls like www.mysite.com/shane and requestes shane.php - So far so good, but when I call www.mysite.com I need it to request index.php
Which rule could I use for this?
Thanks people.
UPDATE - For the sake of future users needing similar help.
My rule of ^([a-zA-Z0-9_-]+)?$ $1.php was causing the problem I mentioned above. The question mark ? is not needed in this type of rule, when you remove the question mark the index.php file in the directory returns to being the default landing page. Thanks to Tomalak for the Help.

You don't need any rule for it. Just make sure that .php default index files for the directory and it'll happen automatically.
Update
I think that your rule
^([a-zA-Z0-9_-]+)?$ $1.php
should in fact be
^([a-zA-Z0-9_-]+)$ $1.php
so that it does not conflict with requests where the filename isn't given (which you want to direct to index.php).

I think this is what you're looking for,not sure thought:
RewriteRule ^/$ index.php

You can either do as Tomalak says, and ensure the DirectoryIndex is set to load index.php.
Or close to what James says,
RewriteRule ^/?$ index.php [L]

Related

Rewrite within a folder

This is very basic but still eludes me, even after studying the relevant stackoverflow pages.
I want this
mysite.net/special/
to return this (the index here)
mysite.net/info/special/
showing the requested address in the browser, not the longer actual address.
The closest I’ve got (with this in the root htaccess) is
RewriteRule ^special/$ info/special/ {QUERY_STRING}
but it’s wrong.
I had thought this is a direct match-and-replace, but apparently not.
RewriteEngine is on.
Guidance appreciated. Thank you.
RewriteRule ^special/$ info/special/ [R=301,QSA,L]
RewriteRule Flags:
R=301: redirect permanently
QSA: combine query string
L: last rule
I am assuming index as index.php for your index file. we are rewriting to your existing url.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/special/$
RewriteRule ^ info/special/index.php [L]

Redirect Specific non-existing URL to an existing

I have a simple need to redirect:
mypage.com/myurl (which doesn't not exist; meening that returns a 404 error)
To
mypage.com/newurl/myurl (which is an exitsing page with content).
I saw a tons of forum post about this, but didn't find an awnser. I tried lot of thinks but with internal server error, infinitive loops etc.
Please note that I have a www to non www permanent redirect, like this:
RewriteCond %{HTTP_HOST} ^www\.mypage\.si [NC]
RewriteRule ^(.*)$ http://mypage.si/$1 [L,R=301]
UPDATE:
I will be specific:
This URL doesn't exist: http://domodom.si/sl/nepremicnine
and needs to be redirected here: http://domodom.si/sl/nepremicnine/nepremicnine-ponudba
Hope this will offer more insights about the problem.
Thanks
assuming mod_rewrite
RedirectPermanent /myurl/ mypage.com/newurl/myurl
Add this to your .htaccess file
RewriteBase /
RewriteRule ^(.*)$ newurl/$1 [NC,L]
you may want to try implementing the answer of nwellnhof in this thread.. it worked for me..

.htaccess redirect whole website structure but leave some of the old structure intact

I have searched but can't quite find the answer to this exact situation:
My website structure is as follows
www.example.co.uk/old-folder1/old-folder2/old-page-name
I needed to redirect the whole structure to:
www.example.co.uk/new-folder1/old-folder2/old-page-name
I successfully did this with:
RewriteRule ^old-folder1/(.*)$ /new-folder1/$1 [R=302,L]
However, if possible for the moment I still want to serve images from the old structure ie.
www.example.co.uk/old-folder1/old-images-folder/old-image.jpg
At the moment the above is being rewritten as:
www.example.co.uk/new-folder1/old-images-folder/old-image.jpg
Which makes sense but this leads me to my question, is there any way of excluding some of the sub directories in 'old-folder1' from the RewriteRule above so that for example www.example.co.uk/old-folder1/old-images-folder/old-image.jpg is still accessible?
I don't have much experience with this but from research I came up with the following but it doesn't work.
RewriteCond %{REQUEST_URI} !^/old-folder1/old-images-folder/.*$
I am beginning to think this might not even be possible with the approach I have taken with:
RewriteRule ^old-folder1/(.*)$ /new-folder1/$1 [R=302,L]
Thanks
Entire contents of the .htaccess file at the moment is:
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/old-folder1/old-images-folder/.*$
RewriteRule ^old-folder1/(.*)$ /new-folder1/$1 [R=302,L]
Try:
RewriteRule ^old-folder1/(?!old-images-folder)(.*)$ /new-folder1/$1 [R=302,L]
HOwever, what's probably happening is that your images are linked using relative URLs, so they may be relatively linked to the "new" images folder.
You should check your links and make sure they're linked correctly to the old path.

.htaccess url rewrite rule to subdirectory not working

I want this: RewriteRule ^paper$ /express/index.php?c=p [L] but it does not work. Instead, it's trying to send me to /index.php?c=p, ignoring the subdirectory express altogether. However, RewriteRule ^express/paper$ /express/index.php?c=p [L] works. This is my .htaccess code:
RewriteEngine on
RewriteBase /
RewriteRule ^paper$ /express/index.php?c=p [L]
RewriteRule ^express/paper$ /express/index.php?c=p [L]
The url I WANT is just http://site.com/paper but I have to do http://site.com/express/paper to get the rewrite rule to work.
And yes, I only use one of those rewrite rules at a time.
UPDATE
Well, I'm not really getting any responses, so I'm going to go ahead and close this browser tab. I'll check back now and then but don't be offended if it takes a little while. Thanks for any help offered.

Remove string in .htaccess

I have the following entry in my .htaccess file:
RewriteRule ^blogs$ ?name=data&case=gview&group_id=31%1 [L]
What I do is, I redirect blogs to ?name=data&case=gview&group_id=31
Now what happens is, all my urls are now blogs?id=1 etc, but I need them to stay just ?id=1
How can I remove the blogs from rest of the urls?
This what I came up with, but it doesn't work:
RewriteRule ^blogs?(.*)$ /$1 [L]
EDIT I might be explaining it wrong. I need to change the actual url display of the links. Is that actually possible?
Not sure... but is that what you're looking for ?
RewriteRule ^/?$ /blogs [L,QSA]
What's wrong with just adding a R to your original rule?
RewriteRule ^blogs$ ?name=data&case=gview&group_id=31%1 [L,R=301]
Ok, the answer is pretty much: It can't be done. I just went over my code, added / before main urls and all is working as intended.
Thank you both for suggestions.

Resources