.htaccess not working when url contains a path - .htaccess

I've searched stackoverflow and numerous websites for an answer to this. So please don't shoot me if it's already been answered on stackoverflow. I am stuck.
I just need a simple redirect where anything after my website name is put into a querystring of $target except for when a path is included in the url.
It doesn't work when a path is given.
eg)
mysite.com.au/home to mysite.com.au/?target=home
mysite.com.au/home/ to mysite.com.au/?target=home
mysite.com.au/home/something to mysite.com.au/home/something
mysite.com.au/home/something/ to mysite.com.au/home/something
mysite.com.au/home/something/file.php to mysite.com.au/home/something/file.php
The below in my .htaccess file. It only works for condition 1 above.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ?target=$1
Can somebody show me a better/proper way for url redirects or know how to do this?
I don't want to use specific 301 redirects.
Thanks for any help you can provide.

You can put this code in your htaccess (which has to be in document root folder)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /?target=$1 [L]

Related

URL rewriting not working from index.php?id=2 to /2

I have a URL called
http://localhost:8080/text/index.php?id=2
and I have to redirect on
http://localhost:8080/text/2
So I added the below code in the .htaccess but it's not working
RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [QSA]
I refer to the below two links. Is there any issue with my code?
common-htaccess-redirects-19-6-2018 and htaccess-rules
After suggested answer, I tried below code
HTML
Register
login
or
Register
login
.htaccess code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[\w-]+/(\d+)/?$ index.php?id=$1 [QSA,L]
With your shown samples, please try following. Please make sure to clear your browser cache before testing your URLs. This considers that you are
hitting URL http://localhost:8080/text/2 in browser.
##Making RewriteEngine ON here.
RewriteEngine ON
##Placing conditions to check if these are non-existing pages only.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##Writing rule to rewrite to index.php with needed variable here.
RewriteRule ^[\w-]+/(\d+)/?$ index.php?id=$1 [QSA,L]
Issues in OP's attempt: You are trying to attempt to match digits in starting where your url doesn't have digits, so rather use [\w-]+ with it. Also use QSA,L flags with your rewrite rule to handle query string and come out of the rule in case this is executing.

htaccess rewrite for routing from a sub directory

I've been using this .htaccess file for enabling the grav Cms for a few sites:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/grav/
RewriteRule ^$ /grav [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /grav/$1 [L,QSA]
This works flawlessly as long the site is at the document root.
Now, I have a new site that starts in 2020/ and has Grav installed in 2020/grav.
I modified the above .htaccess to be:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/2020/grav/
RewriteRule ^2020$ /2020/grav [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^2020/(.*)$ /2020/grav/$1 [L,QSA]
This .htaccess file is located in /2020/.
While in the first case the url is left as is and Grav gets the rest of the url as parameters, in the second case (2020/) the url changes to /2020/grav, which is not what I want?
I wanted to check if it was a Grav issue and I've setup a simple test case, with the above .htaccess file and a grav/ directory with only an index.php file in there and I got the same result.
Any hint on how I have to modify the .htaccess file to get 2020/abc to stay as is in the url bar and at the same time have Grav to get the abc argument?
You can have this code in 2020/.htaccess:
RewriteEngine On
RewriteRule ^$ grav/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!grave/)(.+)$ grav/$1 [L,NC]
(?!grave/) is negative lookahead condition that will skip match if match already starts with grave/
The original question was about getting it to work with an .htaccess.
#anubhava gave a very good answer for the general case. But it sadly was not enough for Grav.
I got some more help in the Grav forums and at the end of a long journey Ole found out that the question has been asked in their forums a long time ago and a solution was found!
The /2020/.haccess can be simply defined as:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/2020/grav/
RewriteRule ^(.*)$ /2020/grav/$1
But what one need is to add a couple of configurations in the /2020/grav/user/config/system.yaml file:
custom_base_url: 'http://domain.tld/2020'
session:
path: /2020
You can find a more detailed question and Ole's reply in the Grav forums

rewrite rule is not redirecting properly

I would like to be able to use rewrite rules to redirect anyone who opens the link :
domain.com/name
to
index.php?username=name
what would be the best way to do it?
I tried to post the htaccess code I wrote but stackoverflow keeps saying it doesn't meet standards so I removed it.
thanks in advance
Something like this will do it:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)/? [NC]
RewriteRule .* index.php?username=%1 [L,QSA]
It will map silently
http://domain.com/name
To
http://domain.com/index.php?username=name
For this to work, /name must be the first directory in the incoming URL path.

Expression Engine hide index.php not working correctly

I am working on a site that was working before someone else made some changes to the site. Now, I have to include index.php to the URL before the contact form will work.
Here is the htaccess file code I am using:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
What am I missing here?
Thanks.
The ? after index.php indicates to me that you're running PHP as CGI ... perhaps that was changed?
Also see: https://stackoverflow.com/a/11143216/174299

Rewriting URL using .htaccess and mod_rewrite

I used this code in my .htaccess to rewrite my urls which is like this:
www.example.com/subcategory.php?subcat=my-test
to
www.example.com/my-test
.htaccess code I used:
RewriteEngine on
RewriteCond $1 !^(subcategory\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ subcategory.php?subcat=$1 [L,QSA]
My problem is whenever I access the url not pertaining to my subcategory.php file like contactus and aboutus, the .htaccess file will somehow put me to subcategory.php file which is not what I want. I want my contactus be handled by contactus.php and aboutus with aboutus.php.
I know that there is something wrong with my .htaccess file but I couldn't fix it by myself for I am not so familiar with the .htaccess coding.
Any suggestion would be greatly appreciated.
Thank you very much!
You are rewriting all requests to subcategory.php. There is no way for .htaccess to tell whether /xxx is a subcategory or some different page, so you could redirect it to a different script.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
Use this code snippet instead of yours and move all the "routing" logic to PHP. In PHP, you can find out whether /xxx is a subcategory, a contact page, an article or something else and use a script suitable for that kind of database record.
You will find the requested URL in $_SERVER['REQUEST_URI'].

Resources