I have big set of links - example:
tempfile-1.php
tempfile-2.php
tempfile-3.php
...
tempfile-255.php
What I need to do is to redirect the files respectively to:
temp-file-1.php
temp-file-2.php
temp-file-3.php
...
temp-file-255.php
I know how to rewrite it one by one, but that is not as solution.
Can you help to rewrite these type of links in wildcard rule/cond?
Thanks a lot in Advance.
Assuming keyword file is present in all the links, you can use this single generic redirect rule in your site root .htaccess to redirect all links:
RewriteEngine On
RewriteRule ^(temp)(file-.+\.php)$ /$1-$2 [L,NC,R=301]
(temp.+) is 1st group that matches starting temp
file-\d+\.php is 2nd captured group that starts with file and ends with .php
Try it like this, when working correctly you can change R=302 to R=301.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)(file-\d+).php$ $1-file-$2.php [R=302,L]
Related
I'm using .htaccess to rewrite URLs with two groupings.
My .htaccess:
RewriteRule ^tips/([0-9]*)/?([0-9]*)?/?(.+)?$ /tips.php?item=$1&id=$2 [L,QSA]
It works correctly when there are two numbers are in the URL, but when one is empty, it skips the capture group entirely.
example.com/tips/1/2/abc -> /tips.php?item=1&id=2 (EXPECTED)
example.com/tips//2/abc -> /tips.php?item=2&id= (UNEXPECTED)
/tips.php?item=&id=2 (WHAT WAS EXPECTED)
I've put this into a few regex/htaccess testers but they all seem to say it should be working as I expected.
(This of course is an unusual URL, but I want to pass it to PHP so I can handle the error in PHP.
You may use this rewrite rule with a THE_REQUEST variable:
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+tips/(\d*)/(\d*)/ [NC]
RewriteRule ^ tips.php?item=%1&id=%2 [L,QSA]
mod_rewrite engine converts multiple // into a single / in RewriteRule pattern therefore we need to use THE_REQUEST here that matches against original request received in Apache.
With your shown samples, could you please try following.
Options -MultiViews
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tips/(\d+)/(\d+)/?$ tips.php?item=$1&id=$2 [L,QSA]
This will look for non existing files/directories and rewrite them to php file with parameters, like #anubhava sir mentioned multiple /slashes will be converted to single / by rewrite rule so this should work for both of your mentioned cases.
I'm trying to change this query "https://myurl.com/puff/?search_keyword=xyz" into this one "https://myurl.com/puff/?s=xyz".
I saw an answer similar to this, but I haven't used regular expressions before and had issues customizing it to what I need. I'm using WordPress and editing the .htaccess file to achieve this.
I looked into How to replace the first parameter name in query string using .htaccess? and tried to use that to customize my own .htaccess but had issues implementing it on my own.
This is what my current .htaccess file looks like:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /puffadvisor/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /puffadvisor/index.php [L]x
</IfModule>
# END WordPress
All help is greatly appreciated :) thank you so much.
You need a condition based on %QUERY_STRING and then references to the capture groups prefixed by %
RewriteEngine On
RewriteCond %{QUERY_STRING} (.*(?:^|&))seach_keyword((?:$|=|&).*)
RewriteRule /puff /puff?%1s%2
This {DOCUMENT_ROOT}/.htaccess replaces the parameter name search_keyword with s on /puff location, no matter at which position the parameter occurs.
You might want to rewrite all pathes
RewriteRule .* $0?%1s%2
I am using below code on my .htaccess file
RewriteRule ^([^/]*)/([^/]*)$ /view_basket.php?order_id=$1&pin=$2 [L]
the goal is to redirect a clean URL like below
http://www.zire20.ir/77438/9512
to this one
http://www.zire20.ir/view_basket.php?order_id=77438&pin=9512
The thing is it was working on my previous server but now I changed to godaddy hosting and it's not working! any idea ?
p.s:
and my whole .htaccess file is like below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^zire20.ir [NC]
RewriteRule ^(.*)$ http://www.zire20.ir/$1 [L,R=301]
RewriteRule ^([^/]*)/([^/]*)$ /view_basket.php?order_id=$1&pin=$2 [L]
RewriteRule ^([^/]*)/([^/]*)$ /view_basket.php?order_id=$1&pin=$2 [L]
lots of photos are not loading!
The problem with your current rule is that you are rewriting unconditionally. Any URL that contains a single slash will get rewritten. I imagine that some of your (static) photo URLs match this pattern.
Common practise is to only rewrite the URL if it doesn't match an existing file (or directory):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /view_basket.php?order_id=$1&pin=$2 [L]
This makes sure the request is only rewritten for non-existing files (not a file or a directory). I've also made the pattern a little more restrictive so there must be 1 or more chars before and after the slash (+), instead of 0 or more (*).
The thing is it was working on my previous server
I can't see how this was possible, unless the URL structure was different on the previous server?
After banging my head against this for the better part of a week, it turned out to be the same problem, and solution, as in this thread: RewriteCond in .htaccess with negated regex condition doesn't work?
TL;DR: I had deleted my 404 document at some point. This was causing Apache to run through the rules again when it tried to serve the new page and couldn't. On the second trip through, it would always match my special conditions.
I'm having endless trouble with this regex, and I don't know whether it's because I'm missing something about RewriteCond or what.
Simply, I want to match only top-level requests, meaning any request with no subdirectory. For example I want to match site.com/index.html, but not site.com/subdirectory/index.html.
I thought I would be able to accomplish it with this:
RewriteCond %{REQUEST_URI} !/[^/]+/.*
The interesting thing is, it doesn't work but the reverse does. For example:
RewriteCond %{REQUEST_URI} /[^/]+/.*
That will detect when there is a subdirectory. And it will omit top-level requests (site.com/toplevelurl). But when I put the exclamation point in front to reverse the rule (which RewriteCond is supposed to allow), it stops matching anything.
I've tried many different flavors of regex and different patterns that should work, but none seem to. Any help would be appreciated. this Stack Overflow answer seems like it should answer it but does not work for me.
I've also tested it with this .htaccess rule tester, and my patterns work in the tester, they just don't work on the actual server.
Edit: by request, here is my .htaccess. It allows URLs without file extensions and also does something similar to a custom 404 page (although its purpose is to allow filenames as arguments, not be a 404 replacement).
Options +FollowSymLinks
DirectoryIndex index.php index.html index.htm
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} =/home/me/public_html/site/
RewriteRule ^(.*)$ index.php
RewriteCond %{REQUEST_FILENAME} !-f # Below this is where I would like the new rule
RewriteRule ^(.*)$ newurl.php
</IfModule>
I want to match site.com/index.html, but not site.com/subdirectory/index.html
You can use:
RewriteRule ^[^/]+/?$
Or using RewriteCond:
RewriteCond %{REQUEST_URI} ^/[^/]+/?$
I am looking to find a way to match a URL like www.domain.tld/about or www.domain.tld/contact within my .htaccess file. The rule has to be dynamic as the pages come from a CMS so the rule needs to be able to accept any newly created page.
Currently I have the following rule:
RewriteRule ([^/]+)$ ?cat=generic&page=$1 [L]
The issue is that without a trailing / or anything else to help identify the catch, it just triggers a 404 error page. I used to have the rewrite as:
RewriteRule ([^/]+)/$ ?cat=generic&page=$1 [L]
but decided to not have trailing slashes on the end of URL's, unless its a folder path.
Thank you anyone who can help on the issue.
Put this rule on top of all other rules i your .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^broadleaf$ /products/desktops/broadleaf-one [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /?cat=generic&page=$1 [L,QSA]
/?$ makes trailing slash optional.