URL rewriting breaks when index.php?url=index (parameter is "index"). Why? - .htaccess

Weird issue: I'm using this .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
to get these URLs. Works perfectly.
http://www.example.com/cool/url
Problem
But when the ?url-parameter has the word index in it, like in
http://www.example.com/index/page
then $_GET["url"] is empty. I think my RewriteRule is broken (despite i've seen exactly this RewriteRule in lots of tutorials) and the removing of index.php from the URL also removes the index-parameter.
Question
How to fix, how to make URLs like index/page possible ?

This is due to MultiViews option.
Add this line on top of your .htaccess file to disable MultiViews:
Options -MultiViews

Related

When trying to hiding file extensions in URL the index file doesn't work anymore

I've been trying to make adding the '.php' file extension optional in URLs.
So if I had an URL like 'www.example.com/page.php', 'www.example.com/page' would also work. And I managed to do it (this article and this StackOverflow question is where I tried to take solutions from).
Here for example is the solution I found on the article page:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But there was a problem, because I had an URL that was like 'www.example.com/articles/index.php'. While 'www.example.com/articles/index' worked, 'www.example.com/articles' would no longer lead to the index page, even after I tried to add a DirectoryIndex (with values like 'index.php' or even just 'index').
Is there some other way to hide a file extension like '.php', while still making the index file work as intended?
You can use:
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)/?$ $1.php [L]
Who tests if the php file exists
You can do this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)$ $1.php [NE,L]
Only one file test instead of three, so more efficient than the other answer.

Htaccess Mod Rewrite issue

I want the following url:
http://localhost/new/post?url=sample-post-one
to Look Like this via htaccess mod_rewrite:
http://localhost/new/post/sample-post-one/
This might be a question asked already or a similar one but I have been trying to figure it out since a couple of hours and did not get to a solution.
Any help will be highly appreciated. Thanks!
Update
Here's what I've tried
<Files .htaccess,.svn> order allow,deny deny from all </Files>
Options +FollowSymlinks
# For Removing .php extension from files
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# Rewrite rule
RewriteRule ^post/([a-zA-Z0-9_-]+)/?$ post?url=$1 [L]
This should work :
RewriteEngine on
RewriteRule ^post/([^/.]+)/?$ post?url=$1 [L]
Or better ...
RewriteEngine on
RewriteRule ^post/([a-zA-Z0-9_-]+)/?$ post?url=$1 [L]
By the way, you should have a look to guides like this one.
You can use below code:
http://localhost/new/post?url=sample-post-one
RewriteRule ^post/([0-9]+)/?$ post?url=$1 [NC,L] # Handle post requests
and to get more information you can refer to this
Basically we're capturing the bit for the last folder. The rest can be hard coded. Also you should delete the competing rule. ^ indicates beginning of string. $ is the end of the string. If you use both of those characters everything has to literally match except for the bits where we're using REGEX patterns for matching. Also everything is case sensitive.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^new/post/([^/]+)/?$ new/post?url=$1
If you know that you're only going to allow letters, numbers, periods, and hyphens, then you can do this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^new/post/([^A-Za-z0-9.-]+)/?$ new/post?url=$1
If for some reason you're including new/post and post is really your document root it should look like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^A-Za-z0-9.-]+)/?$ ?url=$1
Apache is a well documented application. They provide an excellent resource with hundreds of examples on their site with explanations of each in the mod_rewrite documentation.

mod-rewrite forwarding without changing URL

I have a small problem with my Apache configuration when creating "pretty" URLs. I've got it to the stage where typing (or linkig for that matter) to
index.html
forwards you to
index.php?pageID=Forside
that is exactly what I want.
But how can I get index.html to stay in the address bar of the browser?
Right now it forwards and changes the URL to the original one.
Here my .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} index\.html
RewriteRule .* http://www.radoor-designs.dk/index.php?pageID=Forside [L]
And before someone comments on it: Options +FollowSymLinks is missing since it triggers an error 500 on a one.com webhotel.
Thanks in advance!
Try the following:
RewriteEngine On
RewriteRule ^index\.html$ /index.php?pageID=Forside [L]
I think this may help you to resolve your problem.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.html$ /index.php?pageID=Forside [L]
This will do the redirect for you whilst showing index.html in the browser window.
Strange that symbolic links creates an error 500,
if you want it to redirect to index.html?pageID=Forside then do
RewriteRule .* /index.html?pageID=Forside [QSA,L,R=301]
I'm not 100% certain what you are trying to achieve with this could you explain a little more?

using - instead of _ (underscore) with .htaccess

I'm using with good results the following code to access alla of my php files into the /it directory without specifying the extension. In other words I can access to "http://www.mydomain.com/it/about.php" just writing "http://www.mydomain.com/it/about".
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.mydomain.com/it/$1.php [L]
the same happen when i try to access to http://www.mydomain.com/it/question_answers.php.
How can I access directly to *"http://www.mydomain.com/it/question_answers.php"* also writing "http://www.mydomain.com/it/question-answers"?
I wrote the floowing code below the previous but it seems not to work.
Redirect 301 /question-answer http://www.mydomain.com/it/question_answer.php
because if i write "http://www.mydomain.com/it/question-answer" the browser try to open the page:
"http://www.mydomain.com/it/question-answer.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php"
A small abstract of the post:
I have the page *"http://www.mydomain.com/it/question_answers.php"*
with the first part of code I can get it using the link *"http://www.mydomain.com/it/question_answers"*
I'd like to access the same page also with the following "http://www.mydomain.com/it/question-answers"
Thanks!
This should work for you:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.mydomain.com/$1.php
RewriteRule it/question-answer\.php http://www.mydomain.com/it/question_answer.php [R=301,L]
I have changed two things: I deleted it/ in the new URL of the first RewriteRule. Otherwise you would be redirected to it/it/
I also added \.php to the second RewriteRule. I don't really know why, but the RewriteRule seems to replace the pattern instead of redirecting. And if your pattern is it/question-answer and the real url is it/questions-answer.php the .php will not be replaced.

Expression Engine - htaccess to hide index.php is not working. Why?

I have an MSM install with four (licensed) sites. Three of them work perfectly and behave as they should. The fourth one, currently under construction seems to have a mind of its own. The home page shows up but attempts to add additional templates or template groups with content do not display. I only get
The requested URL /template-name/ was not found on this server.
I double checked and made sure the Enable Strict URLs was set to No and the templates are all synched properly. At this point I am repeating myself. Any clues?
Edited
I found out that if I insert index.php into the URL the other pages and templates will show, which leads me to believe that I have something wrong with the htacess file.
Here is the code I am using (which has worked just fine for other sites):
# BEGIN ExpressionEngine Rewrite
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php?$1 [L]
</IfModule>
# END ExpressionEngine Rewrite
Any clues why this is not working correctly?
Turns out it was the htaccess file. I replaced the rewrite noted above with the following and it solved the problem:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

Resources