I'm no htaccess expert. I've rarely used it for more than blocking dodgy spiders and the basic CI routing to remove index.php.
But basically, I'm trying to turn all requests for urls structured like:
https://www.domain.tld/invoice/[1 or more integers]/[alphanumeric
string].pdf
(such as https://www.domain.tld/invoice/674/9b3vpm3cy5nd8irm.pdf)
to
https://www.domain.tld/invoice/fetch/[1 or more integers]/[alphanumeric
string].pdf
(such as https://www.domain.tld/invoice/fetch/674/9b3vpm3cy5nd8irm.pdf)
So far I've got -
RewriteRule ^invoice/(.+)/(.+\.pdf)$ index.php?/invoice/fetch/$1 [L]
but it's not working correctly (unsurprisingly) and mangling requests for https://www.domain.tld/css/pdf.min.css
Can someone please help construct this rewrite rule properly? I realize this can be done easily using CI routing but I need it done in htaccess so this rule is crucial.
There is no easy solution when rewriting slash / items. Nevertheless, you have an easy workaround:
use all your resource files with absolute path:
<link href="/css/pdf.min.css" rel="stylesheet" type="text/css">
instead of, for example,
<link href="../css/pdf.min.css" rel="stylesheet" type="text/css">
This requires a rer-programming of your PHP script, or ASP.NET.
You can always test:
RewriteEngine On
RewriteRule ^invoice/([^/]+)/(.+\.pdf)$ /index.php?/invoice/fetch/$1/$2 [L]
the greedy regex (.+) may cause some problem.
I figured out the problem - another conflicting rewrite rule below was causing issues. I've looked further into regular expressions and have come up with a condition like so:
RewriteCond %{REQUEST_FILENAME} invoice/[0-9]+/[a-z0-9]+.pdf$
RewriteRule ^(.*)$ index.php?/invoice/fetch/$1 [L]
which seems to work great! I welcome suggestions/improvements/comments to this however.
Related
I have the following situation in my .htaccess:
RewriteEngine On
RewriteRule ^laravel/(.*)$ /$1 [R=301,L]
This is specifically made to not allow people to visit my laravel directory.
However, I want to be able to load a specific file from laravel directory into other files, like this:
<script src="/laravel/public/js/app.js" defer></script>
The problem is the following:
The generated URL will have 'laravel' removed from it as per the rule. If I comment that rule, then that line of code that includes app.js will work.
I have tried several things with my .htaccess and searched for a solution, but alas, I am failing to understand, it seems, how .htaccess code really does the things.
Can anyone help with a rule to allow specifically that URL?
Or, if possible, to allow access to the /laravel/public/js/ directory without removing the word 'laravel' from the URL.
Thank you very much!
Instead of doing complex things with checking negated patterns in a RewriteCond or similar, you could just put a rule before this that matches that URL specifically, does no rewriting at all (- in place of substitution URL), and then uses the L flag to indicate that none of the following rules should be evaluated any more.
RewriteRule ^laravel/public/js/app\.js$ - [L]
I am new to .htaccess
I have created many links which open up pages based on a search result.
The link addresses are, each link has a different prid such as 3110, 3111, 3222
http://mywebsite.com/prid-3110/1500-Sq-Ft-Residential-Plot-for-Sale-In-Arya-Shine-City-Irba-Ranchi-For-Rs-9.75-Lakh
I want this link to point to (ignore everything after the second slash)
http://mywebsite.com/prid-3110/
My Current .htaccess rule is
RewriteRule ^prid-([0-9]+)$ views/propertyview.php?property_id=$1
The correct answer is working below
And add this the following so everything else like CSS, Images work because your browser will know from where to start looking for files.
<head> <base href="../"> </head>
You can use:
RewriteRule ^prid-(\d+)(?:/|$) views/propertyview.php?property_id=$1 [NC,L]
With (?:/|$) you avoid prid-3110blabla, but you can use only:
RewriteRule ^prid-(\d+) views/propertyview.php?property_id=$1 [NC,L]
without this test.
I am using scssphp for my css preprocessing and right now my styles look like src="style.php/style.scss". What I was wanting is to use a htaccess to just write in the style and then anything ending in .scss would get run through style.php. So I tried putting this in my home directory.
RewriteEngine On
RewriteRule ^(.*)\.scss$ style.php/$1.scss
I even tried
#RewriteEngine On
#RewriteRule ^(.*)\.scss$ style.php/style.scss
Neither work, something is happening, because my style.scss is loading with a 500 internal server error, but I'm not sure where the error is.
Your rules are looping, try adding an additional check to not rewrite when the URI has style.php in it:
RewriteEngine On
RewriteCond %{REQUEST_URI} !style\.php
RewriteRule ^(.*)\.scss$ style.php/$1.scss
The rewrite engine will continue to loop through your rules until the URI stops chanmging. What's happening with your rule is that a request like /path/style.scss is getting procfessed and rewritten to /style.php/path/style.scss, and the the rewrite engine loops. The second time around, the rule gets applied again and it rewrites to: /style.php/style.php/path/style.scss, etc.
a better regex to get requested file name would be [a-z_A-Z]+.scss
try your htaccess using this syntax
I'm trying to make my dynamic URL's into static looking URL's.
This is a typical URL that I now have:
http://www.somedomain.com/design/index.php?p=about
I would like it to be: http://www.somedomain.com/about
So far, I've gotten it to look like this: http://www.somedomain.com/design/about.html
This is the Rewriterule I'm using: RewriteRule ^([a-z]+).html$ index.php?p=$1 [L]
How would I modify it so it would look like this: http://www.somedomain.com/about?
Thanks for any/all help!!!
Very much appreciated!
Using rewrite rules to give 'static' URI is NEVER a good idea.
A few other ideas you can use:
Make the 'about' page a directory (folder) with a file called index.php or index.html in it. This way the URL shows http://example.com/about/ and the information you wish can still be displayed as needed.
Use the POST method instead of GET methods. This will display as http://example.com/about.php (Note: there is no ? or other parameters behind that.)
Utilize both methods to give a 'seamless' URI on page transitions.
Rick, you're on the right track. You need to read the Apache rewrite documentation. For your docroot/.htaccess start it with:
RewriteEngine On
RewriteBase /
Then generalised version of your rule:
Rewrite Rule ^(\w+)$ index.php?p=$1 [L]
This will rewrite any requests which are for a word string to index.php. You need to be aware that the rewrite engine rescans the .htaccess file if a match has occured so you need to make sure that you don't create a loop. In this case the replacement string has a "." in it and the pattern doesn't, so this won't occur, but for more complex cases you may need to 'guard' the rules with one or more RewriteCond statements. Again, read the Apache documentation.
I'm trying to rewrite part of an url as I've changed a CMS and still want Google to find my articles.
I have:
www.mywebsite.com/vision
www.mywebsite.com/vision/40/some-article-name
and want to rename them:
www.mywebsite.com/news
www.mywebsite.com/news/40/some-article-name
Any hints as to the re-write rules or where I can look? I'd like to change the rules in my .htaccess file.
# Activate Rewrite Engine
RewriteEngine On
# redirect /vision to /news
RewriteRule ^vision$ http://www.mywebsite.com/news [R=301,NC]
# redirect /vision/bla-bla to /news/bla-bla
RewriteRule ^vision/(.*)$ http://www.mywebsite.com/news/$1 [R=301,NC,QSA]
In theory (and practically) these 2 rewrite rules can be combined, but then if you have URL that starts with "vision" (like this, for example: /visions/hurray) then such rule may redirect wrong URLs. Therefore I have done it via 2 rules which is much safer.
Try: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
or: http://httpd.apache.org/docs/2.2/mod/mod_substitute.html if you want to change links in the html content returned to the browser.
Here is an example of how I might do the rewrite I think you're after...
RewriteRule ^(.)/vision/(.)$ $1/news/$2
This may be to broad of a rewrite scope in which case this may be better...
RewriteRule http://www.mywebsite.com/vision/(.*)$ http://www.mywebsite.com/news/$1
Also learning the basics of regex will be a needed skill for doing any complex rewriting IMO.
Hope that helps.