I'm trying to implement a REST-style URL with a mod-rewrite turned on in .htaccess. There's a bit of a kicker which is that I'm developing in a test environment (new cpanel account). Here's the .htaccess:
RewriteEngine on
#REMOVE THIS LINE ON SITE LAUNCH!
RewriteBase /~myNewAccount/
#Hide .php extensions for cleaner URLS
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Options All -Indexes
The URL I CAN use looks like this:
www.example.com/~myNewAccount/index.php/id/50
I can access the PATH_INFO here, but when I try to do this:
www.example.com/~myNewAccount/index/id/50
...I get a 500 internal server error. I've tried implementing the solution found here by Gumbo but that mucks things up.
Ideas on what might be causing this?
Try this rule:
RewriteRule ^index(/.*)?$ index.php$1 [L]
Or if you don’t want index to be in the URL path at all:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php/$0 [L]
Related
I'm running a few .htaccess rewrite rules, to transform the URL path into a index.php query parameter.
So basically when someone visits https://example.com/elections it transforms the URL into https://example.com/?page=elections
Options -Indexes
RewriteEngine on
# Ignore extensions
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|png|gif|svg|pkg)$ [NC]
RewriteRule ^([^/]+/[^/]+/). $1 [R=301,L]
# Rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)$ /index.php?page=$1 [L,QSA]
This works great! There's only one issue, and that is unexpected behavior when there are 3 or more slashes in the URL the visitor entered.
So when someone visits https://example.com/one/two/three it transforms the URL into https://example.com/C:/xampp/htdocs/project/test/test/ (localhost test).
How can I make it so it just returns the regular 404 instead of this unexpected behavior?
(It's odd because it doesn't do that with 2 slashes)
Converting my comment to answer so that solution is easy to find for future visitors.
Problem is due to your first 301 error which causing this unwanted redirect and that too to a wrong target URL.
You can just keep this code in your .htaccess:
Options -Indexes
RewriteEngine on
# Rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?page=$1 [L,QSA]
Make sure to test after clearing browser cache or test in a new browser to avoid old browser cache.
I am trying to rewrite page URL's with .htaccess.
I would like to transform links like this (mysite.com/page.php -> mysite.com/page)
This is the code which I've used in previous sites with the same requirements
RewriteEngine on
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php
But when I try to access mysite.com/page it leeds to a 404 page where as mysite.com/page.php will show the page.
Can anyone explain if I have missed a setting somewhere? I have placed the .htaccess file in the site root dir and the permissions are set as 644.
Instead of using %{REQUEST_FILENAME} which by the way you have an unneeded / there, I suggest you to use %{DOCUMENT_ROOT}/$1\.php, here is an example:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
## To internally redirect /anything to /anything.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^([^/]+)$ $1.php [L]
Additionally on your rule you have /$ which would again cause you trouble as you want to catch /page.
You could have made it /? to make the / optional.
Your .htaccess fixed should look like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([a-z0-9_-\s]+)/?$ /$1.php [NC,L]
I need to get the following URL rewrite syntax correct and am struggling.
xxxxx.com/public_html/food/
Needs to be rewritten as:
xxxxx.com/food/
I tried this and it doesn't work:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/public_html/food/(.*)$
RewriteRule ^(.*)$ http://xxxxx.com/food/%1 [R=301,L]
My client is using Joomla (which I am not familiar with), so I need to do so using the .htaccess file per everything I have researched so far. I am just struggling getting the syntax to work correctly though.
Thanks!
Try this, .htaccess
for Rewrite
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [QSA,L]
for Redirect (add R=301)
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [R=301,QSA,L]
Edit:
If you want to rewrite all urls (including /food)
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public_html/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public_html/$1 [QSA,L]
It will allow you to access site like,
http://xxxxx.com/food/ will point to http://xxxxx.com/public_html/food/
http://xxxxx.com/sample/ will point to http://xxxxx.com/public_html/sample/
http://xxxxx.com/anything/cool/?product=123 will point to http://xxxxx.com/public_html/anything/cool/?product=123/
Okay so i'm working on creating a thing to delete images from my server. I figured out about the unlink function and have turned it into a get variable. How can I use htaccess to delete a file from my server with a url like this example.com/imagename/delete
RewriteRule ^/delete/([^/]+)$ index.php?delete=$1
I realized that this does /delete/imagename but I want the urls to be /imagename/delete. I tried the following but that didn't work.
RewriteRule ^/([^/]+)$/delete index.php?delete=$1
I know this sounds simple and easy to find on the web but I can't seem to find this!
Change your rules to
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]+)/delete/?$ index.php?delete=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.png [L]
I have a URL with a parameter which I wish to make into sef URL:
want:
http://map.tautktiv.com/street.php?address=abc
to become:
http://map.tautktiv.com/street/address/abc
or
http://map.tautktiv.com/address/abc
have tried several online tools to generate a .htaccess rule, but none of them have any effect on the URL, .htaccess file is active (tried to put some gibberish in it and got error 500)
these are the rules I tried:
1.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^address-([^-]*)$ /street.php?address=$1 [L]
RewriteRule street/address/(.*) street.php?address=$1
2.
Options +FollowSymLinks
RewriteEngine on
RewriteRule /address/(.*)\.php street.php?address=$1
3.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# add whatever other special conditions you need here
RewriteRule ^/([0-9]+)-(.*)$ /street.php?address=$1 [L]
RewriteRule /(.*)/(.*)/$ street.php?address=$1
the site is a sub-domain which files reside in a sub directory in a shared hosting GoDaddy server, have also tried to apply these rules to the .htaccess in the directory above it, same result.
tried also this per below suggestions
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
same result, nothing happens.
tried to go directly to page from main domain but same result:
http://tautktiv.com/map/streets/street.php?address=abc
First rule will redirect your ugly URL to the pretty URL.
Second rule will internally redirect it back so the user will not see the ugly URL.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Internally forward /street/address/abc to /street.php?address=abc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^street/address/(.*)/?$ /street.php?address=$1 [NC,L]
# Internally forward /address/abc to /street.php?address=abc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^address/(.*)/?$ /street.php?address=$1 [NC,L]
If you confirm the rule to be working as expected then you can change it from 302 to 301 as you do not want to use 301 until you know the rule is working as expected.
The .htaccess should go inside the folder where street.php is located.
HTTP is US ASCII so your language would fail, it will redirect it to something like this:
/street/address/%25D7%2590%2520%25D7%2598%25D7%2591%25D7%25A8%25D7%2599%2520%25D7%2599%25D7%25A8%25D7%2595%25D7%25A9%25D7%259C%25D7%2599%25D7%259D%2520%25D7%2599%25D7%25A9%25D7%25A8%25D7%2590%25D7%259C
Your best bet here would be to change the links to use /street/address/word instead of the php file directly.
This way you would not need the first rule and you can use only the internal redirect which would work just fine with this update.
Try this one:
RewriteEngine on
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^address/(.*)$ street.php?address=$1 [r=301,L]
In your examples you'd missed ^ and $ in the second row of RewriteRule.
And use [r=301,L] instead of [L] to tell the browser, that thzis is premanent redirecting.