Htaccess conflicts - php

I am trying to do some rewrites with my .htaccess file
All I am trying to do is rewrite the url:
www.example.com/book.php?course=#WHATEVERSLUG
to
www.example.com/course/#WHATEVERSLUG
WHICH I HAVE SUCCESSFULLY BEEN USING THE CODE BELOW FOR A WHILE NOW WITH NO ERRORS
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^course\/([^/\.]+)/?$ book.php?course=$1 [L]
Then, I tried adding another rewrite mod, below:
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
to shorten my urls by removing the .php extensions, and remove the traling slashes and it appears that they do not mix well for some reason. I am not very good with .Htaccess, so any help will be possitive.
Or if someone has another suggestion to rewrite:
www.example.com/book.php?course=#WHATEVERSLUG
to
www.example.com/course/#WHATEVERSLUG
And
Rewrite .php files to remove extenstions and traling slashes..
it'd be so greately appreciated. Thank you in advance.

Figured it out!:
w/ a little a help from: Multiple conflicting htaccess RewriteRules
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^course\/([^/\.]+)/?$ ./book.php?course=$1

Related

htaccess adding .php to the end of $_GET variables when adding slash

I attempted to create a little bit of htaccess which alters a URL from something like
http://localhost/website/page.php?id=_abc-123
to
http://localhost/website/page/_abc-123
It works for the most part, in that I can visit the page without having trouble locating scripts and CSS files. However, if I try to echo out $_GET["id"], instead of getting _abc-123, I will get _abc-123.php.
This is what I have so far within my htaccess file:
Options +FollowSymLinks
# remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
All help is appreciated,
Thanks.
Test movie page first, and test if file with .php exists:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
# remove extensions
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
First of all, if you are not sure where your error lies, you can try online tools for .htaccess like htaccess.mwl.be.
Obviously your first RewriteRule contitions are met, which results in your "error".
With the help of this tool and some knowledge about how regex work, we can fix your .htaccess to this:
Options +FollowSymLinks
# remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
The only thing I changed is removing the "L" flag from your first RewriteRule, because its RewriteCond is met but we need it to go through the second RewriteRule.
For more information about the L-flag have a look at the documentation.

Dynamic url change into seo friendly url

A very common problem, but couldn't fix the issue.
I want
http://website.de/page.php?module=helios
into
http://website.de/page/helios
I have tried lots of .htaccess code like this one, but still page sends to 404.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ page.php?module=$1
Please provide some suggestions.
Try this rule in your site root .htaccess:
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([^/]+)/?$ page.php?module=$1 [L,NC,QSA]
Try...
RewriteRule ^page/([^/]*)/$ /page.php?module=$1 [L]
Use this use your base path
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/(.*)$ page.php?module=$1

Weird .htaccess behaviour

this is my .htaccess-content:
RewriteEngine On
RewriteBase /
Options -Indexes -MultiViews
#Rewriting /profile.php?name=XY to /player/XY
RewriteRule ^player/([^/]*)$ /profile.php?name=$1 [L]
#Remove .php file ending
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
If I am browsing to my-domain/player/XY it redirects me to player.php?name=XY (and prints an internal server error because player.php doesn't exist) instead of showing the profile.
But if I change it to
RewriteRule ^player/([^/]*)$ /profile.php?name=$1 [L] and open my-domain/playera/XY it works fine.
Can you help me, please?
Not sure why you're getting that error since the first rule should match /player/XY. But you can probably add a few conditions to your php rule to ensure that it is rewriting correctly:
#Remove .php file ending
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]

Yii and .htaccess redirect

I want to redirect pages from my_site.com/gamenews.php?id={ID} to my_site.com/news/{ID}/, but can't :(
My .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/index\.php /$1/index [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
RewriteRule ^gamenews\.php\?id=(.*) /news/$1/ [L,R]
AddDefaultCharset UTF-8
Error that displays:
Unable to process request "gamenews.php"
What I'm doing wrong? Thanks.
Try like this
RewriteRule ^news/([0-9]+)/ gamenews.php?id=$1[L,R]
The rewrited url should go first
[0-9] means that second parameter should be a number
The order of your htaccess rules might be your problem in addition to your syntax. Unless I misunderstood your question.
Try this in your htaccess.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^news/(.*) gamenews.php?id=$1 [L]
RewriteRule ^(.*)/index\.php /$1/index [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
AddDefaultCharset UTF-8

apache htaccess codeigniter query string to path

i'm trying to make a codeigniter based website be able to convert index.php?option=test&controller=con1 to /test/con1 using .htaccess i tried multiple examples i found and none seem to work, it goes straight to the home page. any ideas?
i've tried things like
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^\/(.+?)\/(.+?)\/? /index.php/$1/$2 [L]
but doesn't throw any errors or anything.
my current htacces is
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|img|js|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
try put this .htaccess in same directory with your index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Categories