Mod Rewrite append .php to string in URL - .htaccess

OK so I've found every rendition of this but not this specifically. So my urls are in this format
www.inspection.com/users/?action=register
I want to convert it to
www.inspection.com/users.php?action=register
Here's what I've got so far in my htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[A-Za-z0-9]+/\?action=[A-Za-z0-9]+$ $1.php?action=$2 [L,QSA]
But error logs say RewriteRule: cannot compile regular expression
Main Goal: After the domain name is the reference to the file I want to call. So append php on the end of that string but keep the query parameters

With your shown samples, attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs. Also make sure that your .htaccess rules file and .php file are residing on same folder.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/?]*)/?\?action=(.+)$ $1.php?action=$2 [NC,L,QSA]
One more thing, you have not created a capturing group on left side of RewriteRule hence you can't use them on the right side of it.

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.

URL Rewriting using .htaccess

I'm trying to get a url to rewrite using htaccess but can't seem to get it working.
I'm trying to rewrite http://website.com/pages/blog/article.php?article=blog-entry so that it can be entered as http://website.com/pages/blog/blog-entry but i'm getting an error when I try the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^pages/blog/(.+)$ pages/blog/article.php?article=$1 [NC,L]
Can anybody see where i'm going wrong as this just gives me a 404 error. Thanks in advance.
Use this rule inside /pages/blog/.htaccess:
RewriteEngine on
RewriteBase /pages/blog/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ article.php?article=$1 [QSA,L]
I'm trying to rewrite
http://website.com/pages/blog.php?article=blog-entry so that it can be
entered as http://website.com/pages/blog/blog-entry but i'm getting an
error when I try the following:
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond
%{REQUEST_FILENAME}\.php -f
RewriteRule ^pages/blog/(.+)$ pages/blog/article.php?article=$1
[NC,L]
Your wording is confusing, but I believe this is what you mean:
The real url is: http://website.com/pages/blog.php?article=blog-entry
you want to be able to use a 'friendly' url: http://website.com/pages/blog/blog-entry to point to the real url.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^pages/blog/(.+)$ /pages/blog/article.php?article=$1 [QSA,L]
The first two tests ask: is this a directory that exists? is this a file that exists? Because article.php is a file, it won't be included in this action, so you won't enter into an endless loop, which is always the risk with incorrectly done rewrite rules.
Take the given url, and use query string append (QSA) to attach the desired data to the actual file that will process the request. This is not a rewrite in that the url the user sees does not change, this only happens internally in apache, which sends the request to the desired target, with the desired information.
You have to test if the file or directory exists because otherwise you'd be applying this rule incorrectly, since it should only be applied when the target does NOT exist. This is basically how all blog/cms 'search engine friendly urls' work, more or less.
Last, since the target is /blog.php?article=blog-entry you can't skip the leading /.
However, it's unclear to me why you'd want the friendly url to be so long, when you can just make it short, and friendlier: like, pages/[article-name]

How can I rewrite an already rewritten file if it is not found

I need to rewrite a URL for an image file. This is the easy part and looks like this:
RewriteRule ^img[/]?(/[^.]+)?$ - [L]
RewriteRule ^img/([A-Za-z0-9_-\s]+)/([A-Za-z0-9_\-\s\.]+)?$ cache/$1-$2 [L]
However if the image file is not found in that cache directory I need to rewrite this again to a PHP script that will create the image and save it in that cache directory.
I tried the following rewrite, but it doesn't work as intended. Instead all image URLs are rewritten to the PHP file regardless if the file exists in the cache or not.
#RewriteCond cache/%{REQUEST_FILENAME} !-f
#RewriteCond cache/%{REQUEST_FILENAME} !-d
#RewriteRule ^cache/([A-Za-z0-9_\s]+)-([A-Za-z0-9_\-\s\.]+)?$ createimg.php?type=1&hash=$1&filename=$2 [L]
Any ideas? Thanks.
Since after the first rule, the same rules will be matched with the new url, the url already contains "cache/". %{REQUEST_FILENAME} and other magic variables are updated accordingly. Therefore you don't need to put "cache/" in front of the variable.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cache/([A-Za-z0-9_\s]+)-([A-Za-z0-9_\-\s\.]+)?$ createimg.php?type=1&hash=$1&filename=$2 [L]

Basic URL rewriting: How to have a css link, img src, etc, ignore it?

I have a website running at localhost/pm and the RewriteBase is correctly set to /pm/. There is a link tag in my document: <link ... href="themes/default/css/default.css">.
When the url is localhost/pm or localhost/pm/foo the CSS works all right. When there are more slashes in the URL, however, like localhost/pm/foo/bar the relative URL if the stylesheet changes to foo/themes/default/css/default.css.
How do I get this to work without having to put some sort of PHP path resolution in the link tag?
# invoke rewrite engine
RewriteEngine On
RewriteBase /pm/
# Protect application and system files from being viewed
RewriteRule ^(?:system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
EDIT:
Basically what I need now is this:
If request contains folder name /themes/ scrap everything that is before /themes/ and rewrite the rest to /pm/themes/...
I tried it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/$1 but I get an internal server error. Why?
If I do it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/ (ie. just remove $1 from the end) and use the URL http://localhost/pm/foo/themes/foo/ the resulting physical location is http://localhost/pm/themes which is what is expected too, which in turn means that at least my regex is correct. What am I missing?
The RewriteRule is almost correct
RewriteRule ^.*(/themes/.*)$ /pm/themes/$1
This rewrites http://localhost/pm/foo/themes/default/css/default.css to http://localhost/pm/themes/themes/default/css/default.css, which is one themes too much. Use this instead
RewriteRule /themes/(.*)$ /pm/themes/$1 [L]
But now you have an endless rewrite loop, because /pm/themes/.. is rewritten again and again. To prevent this, you need a RewriteCond excluding /pm/themes
RewriteCond %{REQUEST_URI} !^/pm/themes/
RewriteRule /themes/(.*)$ /pm/themes/$1 [L]
Now the request is rewritten only once and you're done.
You probably need to add the following lines before your RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
It will only evaluate your rewrite rule if the requested file or directory doesn't exist.
You should post your .htaccess file so we can offer better advice

mod rewrite rule to convert everything after domain to a get query string

I need some help with rewrite rules in htaccess files. I want everything after the domain, after the first slash to be rewritten to get the query string.
If you take a look at mod rewrite everything after domain into get
this is pretty much what I want except that I believe that one of my rules (rewrites files to have a php extension) is interfering with the linked solution.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
What can I do to integrate the linked solution with this rewrite as well?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?query=$1 [L]
English version: If the file or directory does not exist, rewrite to index.php with the query string in $_GET['query'].
For greater flexibility, you could also not pass the request uri into GET with htaccess and just read it directly from $_SERVER['REQUEST_URI'].

Resources