Redirect URLs but ignore if blank - .htaccess

I currently redirect URL's using the code below within a .htaccess file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^news/(.*)$ /news/page.php?ref=$1
This works perfectly and enables me to run a script within page.php to retrieve the correct article. The issue I have is that /news/ gets redirected to page.php, even though no article is specified within the url. How can I modify this code to avoid this.
Currently as a quick fix I have put a condition at the beginning of the page.php file that checks if ref is set and if not then redirect to /news/index.php. This works because I have specified a file, index.php. I would like the url /news/ to resolve without index.php having to be specified.

You can use ^news/(.+)$ in your pattern.
What different between * and + you can read in regex-plus-vs-star-difference

Related

mod_rewrite / regex with unknown folder name

I've read through about 6 or 7 suggested similar post but still can't get this working.
(e.g.:
Reference: mod_rewrite, URL rewriting and "pretty links" explained /
How to write htaccess rewrite rule for seo friendly url
etc...)
I have a PHP website with a help articles section, which pulls the content from the db by article id.
Current URL format =
/sitename/help.php?h=69
I'd like to change the URL format to:
/sitename/help/69/title-of-the-article
Mod_rewrite is enabled on the server, and this seems to work:
RewriteRule ^help/([0-9]+)/?(.*)/?$ /sitename/help.php?h=$1 [NC,L]
/sitename/help/69/random-title loads the content for /sitename/help.php?h=69
However, some of the articles contain images, coming from the path /sitename/images/
The modified URL means the browser tries to call /sitename/images/example.jpg from sitename/help/69/images/example.jpg
The '69' is an unknown variable, (and that seems to break all the examples I've tried!), but should always be a 1, 2 or 3 digit number.
Please can someone help with the rewrite/regex query to remove the /help/{article id}/ from my image URLs?
With your shown samples and attempts please try following .htaccess rules file. Please make sure to keep your .htaccess rules file along with sitename folder(not inside it, alongside it). Also clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sitename/(help)/(\d+)/.+$ sitename/help.php?h=$1 [NC,L]

htaccess rewrite to always redirect to a domain with prefix

I have this project I maintain which I run in XAMPP and by default the url is set to http://localhost and it will take the folder project name inside htdocs to access the content. For instance I have a folder called test so my base url should be http://localhost/test.
The issue I am facing now is, most of the link was written in something like /profile.php and it will redirect to http://localhost/profile.php instead of http://localhost/test/profile.php. I find it hard to do the testing and all when they redirect to the wrong url.
How do I write the htaccess so that it will always read http://localhost/test as the base url?
This is something I tried but since I still quite confuse with the rule so I was not able to achieve what I'm trying to do.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /test/$1 [R=301,L]
EDIT :
All php files are under test folder at 1st level so they can be accessed by browsing as domain-url/any-file.php. In my case it should be something like http://localhost/test as the domain name and /profile.php as first uri also referring to the file profile.php

How to make webpage act like a subdirectory with htaccess

I'm building a website which has a profile page for each user.
I'd like the profile pages to be accessed through visiting a web address, eg:
example.com/profile/username
Obviously it would be impractical to create a new PHP file for each registered user, and so what I'm trying to do is redirect traffic from example.com/profile/(whatever) to example.com/profile, where I can then interrogate the URL and load the correct profile from a database.
However, I'm really struggling to work out how to do this with .htaccess. I've tried all sorts of redirects, etc, but can't seem to get it to work. Can anyone point me in the right direction?
The only other thing I've got in my .htaccess file at the moment is the below to remove .php from URLs:
# Remove .php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Any advice gratefully appreciated!
redirect traffic from example.com/profile/(whatever) to example.com/profile
Note that you should internally rewrite directly to the file that handles the request, which I assume is /profile.php, rather than relying on another rewrite to later append the extension. I assume that code in /profile.php already processes the request as necessary.
I will also assume that "username" can consist only of the characters a-z, A-Z, 0-9, _ (underscore) and - (hyphen). Generally, you want to be as specific as possible.
Try something like the following:
RewriteEngine On
# Disable MultiViews to avoid conflicts with mod_negotiation
# (Since you are using "extensionless URLs")
Options -MultiViews
# Handle requests for `/profile/<username>
RewriteRule ^profile/([\w-]+)$ profile.php [L]
# Append the ".php" extension to any request that does not end in a file extension
RewriteRule ^([^.]+)$ $1.php [L]
Whilst the comment in your code states "Remove .php". The code actually appends the file extension to URLs that don't have one.
This currently only works for files in the document root (as per your original code), eg. /foo. It won't do anything with /foo/bar for instance.
The condition that checks against the file system (ie. RewriteCond %{REQUEST_FILENAME} !-f) was not required in your existing code, unless you have actual files that don't contain a file extension? I'm also assuming you do not need to access filesystem directories directly (as per your original code).
There is no need to escape literal dots when used inside a character class.

htaccess redirect dynamic urls

I am trying to write a htaccess redirect, but it is not working as I want it to.
My current (working) htaccess redirects all html pages with 1 character to the index file as:
RewriteRule ^([a-zA-Z0-9]).html$ index.php?letter=$1
So a.html gets redirected to index.php?letter=a
Now I need to redirect the page a.html?page=2 to index.php?letter=a&page=2
Basically I want to redirect the url, but leave the dynamic part intact.
All of the following return a 404:
RewriteRule ^([a-zA-Z0-9]).html?page=([0-9]+) index.php?letter=$1&page=$2
RewriteRule ^([a-zA-Z0-9]).html?page=(.*) index.php?letter=$1&page=$2
RewriteCond %{QUERY_STRING} page=(.*)
RewriteRule ^([a-zA-Z0-9]).html(.*) index.php?letter=$1&page=%1
I think I'm close, but I can't seem to get there :/
could anyone give me the last push?
Your RewriteRule needs to be
RewriteRule ^([a-zA-Z0-9])\.html$ index.php?letter=$1 [QSA,NC,L]
Please, note that URL parameters are not available for matching within the RewriteRule. If you simply need to append an extra URL parameter you can do so along with the [QSA] flag which would take care of appending the original URL parameters for you.
Please, note that the dot before html needs to be escaped \. as well. The [L] makes sure that rewriting stops and no further rules (if any below) are applied.

Targeting single directory for rewrite rule

I have edited this question to use the actual URLs. I need the url
http://westernmininghistory.com/mine_db/main.php?page=mine_detail&dep_id=10257227
To be rewritten like
http://westernmininghistory.com/mine_detail/10257227/
I have tried
RewriteRule ^([^/]*)/([^/]*)/$ /mine_db/main.php?page=$1&dep_id=$2 [L]
Which works on this page but breaks every other page on the site. I was wondering if there was a way to force the rewriterule to only operate on files within the mine_db directory. I had tried RewriteCond but with no success:
#RewriteCond %{REQUEST_URI} ^/mine_db
I really don't know they proper syntax for this though. Any ideas?
First of your rule can be shortened and written without needing RewriteCond. Also it appears that you want to capture 2 variables after test_db.
You can try this rule instead:
RewriteRule ^(mine_detail)/([0-9]+)/?$ /mine_db/main.php?page=$1&dep_id=$2 [QSA,L,NC]
Which will work with URIs like /mine_detail/12345 (trailing slash is optional). Also note that above rewrite will happen silently (internally) without changing the URLi in browser. If you want to change URL in browser then use R flag as well like this:
RewriteRule ^(mine_detail)/([0-9]+)/?$ /mine_db/main.php?page=$1&dep_id=$2 [QSA,L,NC,R]

Resources