Creating Static Links Using .htaccess - .htaccess

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.

Related

Allow specific URL in htaccess even if htaccess rewrites a word from that URL

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]

why is my RewriteRule not working

I want to be able to Rewrite a following url:
www.mysite.com/en/contact
to something like this
www.mysite.com?l=en&p=contact
but keep the structure.
I tried the following RewriteRule:
RewriteEngine On
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?l=$1&p=$2 [L]
but the browser then wants to load all the included scripts like the css from
www.mysite.com/en
How can I tell the browser, that it shouldn't follow the url link?
Or something like that
Thank you for your help
Chris
There are 2 methods to solve this:
1) Use base tag inside head tag
<base href='/'>
2)Use relative links
<link rel='stylesheet' href='/css/style.css'>
Here the browser looks for the CSS file in the folder in the root named CSS, whatever the URL request may be.

Confusion with htaccess rewrite rule for pdf directory

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.

RedirectMatch issue (redirecting folder, but not contents inside folder)

I have tried this every which way I could, but am not getting any love. I am attempting to redirect a subfolder in the site to just a static page. But I do NOT want to redirect anything ELSE that is inside said folder.
I have tried using the regular redirect 301, but of course that redirects everything in the subfolder.
To sum up: I want to redirect http://www.domain.com/folder/ to http://www.domain.com/foldername2/file.php without redirecting anything else inside /folder/.
The following is what I have in my htaccess... but... it is just ignoring it completely.
RedirectMatch 301 ^/folder$ http://www.domain.com/foldername2/file.php
Thanks in advance for help.
Instead of relying on HTTP, you can use a nearly empty index.html file which contains a <meta> refresh tag in the head section of the document.
The wikipedia article on meta refresh has some good examples, e.g.
<meta http-equiv="refresh" content="0; url=http://example.com/">
The 0 means "zero seconds"—that is, immediately.
After much struggling, I found the answer! It does not use the RedirectMatch, however. It uses a rewrite condition and rule instead.
RewriteCond %{REQUEST_URI} ^/folder/$
Rewriterule ^(.*)$ http://www.domain.com/foldername2/file.php [L,R=301]

Website pages with slashes

Just building a site using php. I would like to know how to create pages without having to make ".php" links for every page. For example I want to build the site to be as follows: www.mysite.com/products instead of www.mysite.com/products.php. (Further example www.mysite.com/products/headphones )
An example of what I am looking for is as follows: http://www.starbucks.ca/store-locator
Any idea how starbucks is doing this? It isn't making a separate folder and index for every page is it?
Thanks in advance!
Jason
You can use URL rewriting to do this. You can create a .htaccess file, place it in the root directory of your website with the following content (as an example):
RewriteEngine On
RewriteRule ^tutorial/([0-9]+)/$ index.php?tutorial=$1
RewriteRule ^page/store-locator/$ index.php?page=store-locator
RewriteRule ^$ index.php
These are some basic rules that tell the web server to rewrite a URL without .php to a URL that does have .php.
In the above example, when you are visiting example.com/tutorial/3/, it will actually visit example.com/index.php?tutorial=3 and then your index.php will display the contents. It's similar for the store-locator page.

Resources