htaccess wildcard redirect to query string - .htaccess

Quick question, and I've seen it asked hundreds of times, but I just can't seem to get it to work (shame on me).
I'm trying to redirect anything other than index.php to view.php?id=$1 where $1 is anything other than index.php - but I can't seem to get it to work.
For example:
http://domain.com/ should use index.php
http://domain.com/index.php as should this
but..
http://domain.com/sdgoi3 should use http://domain.com/view.php?id=sdgoi3 etc
I've tried a few things and gone down through the questions above but to no avail.
Anyone got a solution? Appreciated.

Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteRule ^$ /index.php [L]
RewriteRule ^index\.php - [L]
RewriteRule ^view\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /view.php?id=$1 [L]
The logic here is:
if the request URI is / rewrite to index.php
if the request URI starts with index.php, don't change and pass through
if the request URI starts with view.php, don't change and pass through
if the request is to a non-existing file or directory, pass to view.php with the id param

Maybe something like :
RewriteCond %{REQUEST_URI} !^index\.php
RewriteCond %{REQUEST_URI} !^view\.php
RewriteRule ^(.*)$ view.php?id=$1 [L]
?

Related

Add get variables to url throung htaccess file

I want to add one get parameter to the every url on my wprdpress website except the index page.
For example,
Consider website name to be www.example.com
If www.example.com is hit than nothing should happen
But if I hit www.example.com/events than a get parameter viz. "home" be appended to it. Final URL should be as follows:
www.example.com/events/?home
I want to use this parameter for further processing.
my current .htaccess file contains following
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{QUERY_STRING} !lp-variation-id
RewriteRule ^go/([^/]*)? /wp-content/plugins/landing-pages/modules/module.redirect-ab-testing.php?permalink_name=$1 [QSA,L]
RewriteRule ^landing-page=([^/]*)? /wp-content/plugins/landing-pages/modules/module.redirect-ab-testing.php?permalink_name=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
and i m working on wordpress project.
Try this:
RewriteCond !^/$
RewriteRule ^/(.*)$ /$1?home
Depending on your hosting environment the URL may or may not start with a slash, so you might have to remove it from the RewriteRule.

Trying to change stylesheet url

This is my current .htaccess file:
RewriteEngine On
RewriteBase /application/
RewriteRule (.*)/css/(.*).css www/$1/css/$2.css
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?p=$1
So my end goal here is to have
http://localhost/application/guestbook/css/style.css
forwarded to
/application/www/guestbook/css/style.css
It's almost working, yet when im dumping $_GET i can see that the url he's looking for is
www/www/guestbook/css/style.css
Can someone tell me why its having 2times www/ ? And how can I fix it?
Here is one way to do it:
UPDATED
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^.*/(\w+/\w+/\w+\.css)$
RewriteRule .* application/www/%1 [L]
Will redirect this:
http://localhost/application/anything1/anything2/anything3.css to
http://localhost/application/www/anything1/anything2/anything3.css

Htaccess files and url rewriting

I have 2 questions.
I am currently using wamp server to serve my website.
The homepage is 'localhost/prefix/index.php'
Question 1:
a. I would like it so my home page is:
'localhost/prefix/'
instead of
'localhost/prefix/index.php
b. I would like it so:
'localhost/prefix/admin/profile.php'
is
'localhost/prefix/admin/profile'
How do I go about doing this (I have googled and I am very confused by the syntax)?
Question 2
If I have a url like
'localhost/prefix/games?title=hi'
how can I make it so the url is like this:
'localhost/prefix/games/hi'
Thanks in advance!!
I really have got lost.
EDITED::///
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [R]
Is what I have so far.. It does nothing... But everyone says it should! (the htaccess file is doing something because if I do something random, it throws up errors).
EDITED::///
This seems to remove .php and index.php from the url:
RewriteEngine On
RewriteBase /prefix/
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
Problem now is that my prefix base is not working so it ends up going to
localhost/something/something
rather than
localhost/prefix/something/something
Any ideas?
EDITED::///
I have sussed out that the above code actually works perfectly if the page i'm directing to is in a sub folder. so for example.. this will work:
localhost/prefix/admin/dashboard
But this (because the file is in the root directory, doesn't)
localhost/prefix/login.php
it redirects me to
localhost/login
Any ideas?
EDIT::///
If you are having problems getting it to work. close your browser down and restart... I had caching issues.
This code above will remove .php and also remove index.php.

Rewrite rule to hide folder, doesn't work right without trailing slash

i have a strange apache mod_rewrite problem. I need to hide a sub-directory from the user, but redirect every request to that sub-directory. I found several quite similar issues on stackoverflow, but nothing really fits, so i decided to post a new question.
My .htaccess looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)?$ foo/$1 [QSA,L]
The document-root only contains the following folder/files:
/foo/bar/index.html
I would now expect that example.com/bar and example.com/bar/ would just show me the contents of index.html.
Instead example.com/bar/ show me the content as expected but example.com/bar redirects me with a 301 to example.com/bar/foo/ an then shows the contents. I really don't get why there is a 301 redirect in this case.
When i put something this
RewriteCond %{REQUEST_URI} !^[^.]*/$
RewriteCond %{REQUEST_URI} !^[^.]*\.html$
RewriteCond %{REQUEST_URI} !^[^.]*\.php$
RewriteRule ^(.*)$ $1/ [QSA,L]
on top of that rule it seems to work, but that would require me to list every used file extension...
Is there any other way i can omit the redirect, the folder "bar" should never be seen by an outside user.
Thanks in advance!
1st rewrite rule is redirect from /foo/(.) to ($1) and second - from (.) to $1.
just idea, this has not been tested.
Better late than never...
Got it working with a simple RewriteRule which append a / to every url that doesn't have on.
# only directories
RewriteCond %{REQUEST_FILENAME} !-f
# exclude there directories
RewriteCond %{REQUEST_URI} !^/excluded-dirs
# exclude these extensions
RewriteCond %{REQUEST_URI} !\.excluded-extension$
# exclude request that already have a /
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [R=301,L]

Removing index.php from URL but keep old links working

I'm trying to remove the index.php from the URL, which is working with the .htaccess examples found on the EZPublish site and ForceVirtualHost=true. The problem is that the old links that point to index.php are no longer working (which is problematic when linking from search engines).
I've tried to find a fix for this in using rewrite rules in .htaccess, but I can't get this to work. Some of my attempts are:
Attempt 1
RewriteCond %{REQUEST_URI} ^/index.php
RewriteRule ^index\.php(.*) http://www.mysite.com$1
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule .* index.php [L]
This attempt causes an infinite loop.
Attempt 2
RewriteCond %{REQUEST_URI} ^/(index\.php)?(.*)$
RewriteRule %2 index.php [L]
RewriteRule .* index.php [L]
Also doesn't work :-(.
NB. 'RewriteRule .* index.php [L]' is necessary to make the virtual host setup in EZPublish work.
Any help would be greatly appreciated.
Vivienne
in your settings/override/site.ini.append.php:
[SiteAccessSettings]
ForceVirtualHost=true
Try to check the request line instead:
RewriteCond %{THE_REQUEST} ^GET\ /index\.php
RewriteRule ^index\.php/?([^/].*)?$ /$1 [L,R=301]
RewriteRule !^index\.php$ index.php [L]

Resources