how to remove question mark - php

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /domain.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ /domain.com/$1.php [L]
am using this code for remove question mark but its not working, trying http://domain.com/details?id=71 to http://domain.com/details/id/71
Please help me where am wrong?
Thanks in advance

I'm not sure what you're trying to accomplish with the first rewrite, but for the second (the last three lines), if the incoming URI is not an actual directory or file, you want to rewrite something like details/id/71 to /details.php?id=71?
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [L]
should get you close, assuming there are always 3 fields. In a RewriteRule you don't put the domain name again.

Your code doesn't do anything remotely close to what you want. Your code simply removes the php extension when a request is made explicitly with the extension and adds it back internally. In order to make http://domain.com/details?id=71 get redirected to http://domain.com/details/id/71, you need:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+([^/]+?)(?:\.php|)\?([^=]+)=([^&\ ]+)
RewriteRule ^ /%1/%2/%3? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1?$2=$3 [L]
you want this before your rules that remove the php extension

Assuming your script is called details.php, the following single rule:
RewriteRule details/(.*)/(.*)/$ /details.php?$1=$2
Should rewrite www.domain.com/details/id/71 to www.domain.com/details.php?id=71
You don't need all that other stuff to remove the .php extension.

Related

Custom PHP/SQL CMS htaccess rules

I have made a custom PHP/Sql cms with a dynamic post template called blog-post.php
I am pulling in the content like so:
http://www.example.com/news/blog-post.php?slug=sample-slug-from-database
This allows me to pull in any number of posts using the same template, pulling info from the database.
I have spent 4 hours trying to achieve the following with htaccess ..
Force a trailing slash instead of .php extension
Force blog post urls to show as:
http://www.example.com/sample-slug-from-database/
instead of
http://www.example.com/news/blog-post.php?slug=sample-slug-from-database
Any help is greatly appreciated.
Although your preferred method of URL is to redirect everything, e.g. /sample-slug-from-database/, I think it would be better to instead go with a less strict implementation, e.g. /news/sample-slug-from-database/
Either way, here's the first implementation:
# URL
# INPUT http://www.example.com/random-slug/
# OUTPUT http://www.example.com/news/blog-post.php?slug=random-slug
RewriteEngine On
RewriteRule ^(.*)\/$ news/blog-post.php?slug=$1 [L,QSA]
And the second way:
# URL
# INPUT http://www.example.com/news/random-slug/
# OUTPUT http://www.example.com/news/blog-post.php?slug=random-slug
RewriteEngine On
RewriteRule ^news\/(.*?)(\/(.*?)*)$ /news/blog-post.php?slug=$1 [L]
For anyone else struggling - this is my final code:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteEngine On
RewriteRule ^(.*)\/$ blog-post.php?slug=$1 [L,QSA]

how to remove php extension from the files in root

Hello i want to convert php urls into SEO urls every thing is working fine at my end but the files in the root doesn't work here is the code which i am using in htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ show_staff.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ show_staff.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)$ show_users.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ show_users.php?url=$1
but if i add these lines right after the above lines the files which are in root doenst work without .php extention here is what i want to add here i want to mention without or with using below code the above code works fine for me
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* $0.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
It looks like your stuff is all in the wrong order. Some things:
If you don't have an L flag for a rewrite, the rules after will continue to process the rewritten request. So you want to use the L flag.
Rewrite conditions only apply to the immediately following rule, so you've got a condition that's just dangling somewhere and not being used.
The show_users.php rules will never work the way you've set things up, the regex pattern is the same, so no matter what, your requests will always go to show_staff.php.
Your rules aren't in the right order, two things need to happen if I'm guessing what you're trying to do. You need to match against a request with a php extension and externally redirect the browser, then you need to internally rewrite that request back to the URI with a php extension.
So something like:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \ /+([^/]+)\.php
RewriteRule ^ /%1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
ReweriteRule ^ %{REQUEST_URI}.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9-/]+)/?$ show_staff.php?url=$1 [L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
## hide .php extension
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9/-]+)$ show_staff.php?url=$1 [L,QSA]

issue with URL redirection in Apache (mod_rewrite / mod_alias)

There are many issues related to Apache configuration with mod_rewrite and mod_alias on Stack Exchange but I couldn't find an answer to my issue, so here is another question! ;)
I migrated an old website to a new location.
Articles used to be accessible to an URL such as http://xxx/blog/index.php?post/YYYY/MM/DD/title, so there are many links of that form through the existing webpages.
Now, the URL should be http://xxx/post/YYYY/MM/DD/title, so just remove blog/index.php? from the final URL.
I wanted to use mod_alias and a Redirect clause, but I read here that the Redirect clause was interpreted after the RewriteRule clause, which is a problem in my case because I'm already using a RewriteRule condition.
Here is my current .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
I tried to modify it this way:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/index.php? / [R=301,L]
RewriteRule ^(.*)$ index.php?$1
It almost works except:
http://xxx/blog/index.php?post/YYYY/MM/DD/title is redirected/rewritten to http://xxx/blog/?post/YYYY/MM/DD/title (note the ?) so it doesn't work
it looks like this new line messes up a lot of other URLs that do not start with /blog/index.php? such as the backend URL...
Any help will be more than welcome!
Edit (2014-07-16):
If I use the following rule:
RewriteRule ^/?blog/index\.php(.*)$ /$1 [R=301,L]
then going to
http://xxx/blog/index.php?test
takes me to
http://xxx/?test
which is almost correct (the interrogation mark is still a problem)!
But when I try to match the interrogation mark by adding \?:
RewriteRule ^/?blog/index\.php\?(.*)$ /$1 [R=301,L]
then it just stops working... Going there:
http://xxx/blog/index.php?test
just leaves me there!
Why is that?
Try adding this rule to the beginning of your set of rules:
RewriteCond %{THE_REQUEST} \?post/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^&\ ]*)
RewriteRule ^index\.php$ /post/%1/%2/%3/%4? [L,R=301]
so that it looks like:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \?post/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^&\ ]*)
RewriteRule ^index\.php$ /post/%1/%2/%3/%4? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

How to change weird PHP URL with htaccess code?

Hoping someone can help me here. In my .htaccess file, I've got a rewrite rule written as:
RewriteRule ^/?wrestler/([^/]*)$ /wrestler.php?id=$1 [L]
for a URL that would be something like
localhost/wrestling.php?id=something
And now what I'm trying to do is change a URL like
localhost/category.php?id=something
to
localhost/id/category
The thing to note with this scenario is that this time I'm trying to use the id part as the thing after the first "/" and then add the category part after the second "/".
Below is what my entire .htacces file looks like at the moment.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?article/([^/]*)$ /article.php?id=$1 [L]
RewriteRule ^/?wrestler/([^/]*)$ /wrestler.php?id=$1 [L]
# Removes the .php extension from pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
Any thoughts guys?
You can try
RewriteRule ^([^/]*)/category/?$ /category.php?id=$1 [L]
Try this :
RewriteRule ^([^\/]*)/([^\/]*)$ /$2.php?id=$1 [NC,QSA]
You need two captured parameters.
This expression was tested with http://htaccess.madewithlove.be/.

Rewrite php page with htaccess

I am creating a form at poll.php?code=STRINGHERE
I want to rewrite it with .htaccess to do WEBSITE.com/STRINGHERE
I have done it before but don't remember what I need to do in the .htaccess its something like
RewriteEngine On
RewiteRule ^(\w.+)$ ./poll.php?code=$1
Although I might be wrong. Any help would be great.
Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewiteRule ^([^/]+)$ ./poll.php?code=$1 [L,QSA]
And if you need to redirect the browser when requests are made directly to poll.php:
RewriteCond %{THE_REQUEST} \ /+poll\.php\?code=([^&\ ]+)
RewriteRule ^ /%1? [L,R]

Categories