So this is what I currently have ruled.
RewriteRule ^([A-Za-z0-9-_]+)$ paste.php?p=$1 [L]
Url is like this.
http://website.com/pasteid
What I'm trying to do though is have
example.com/pasteid/download
OR
example.com/pasteid/plain
the GET parameters I'm passing for download / plain are a=download / a=plain
Say you would like to access your site like this:
http://website.com/pasteid/download
If I well understand you, you want this in .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/(.*)$ paste.php?p=$1&a=$2 [L]
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ paste.php?p=$1 [L]
It does this:
http://www.example.com/pasteid/download => /paste.php?p=pasteid&a=download
http://www.example.com/pasteid/plain => /paste.php?p=pasteid&a=plain
The full set of rules let resource files unrewritten (css, js, png, jpg).
Related
I am realy lost here. I am used to work with linux server so I use .htaccess
Now I need to develop a project in windows server and I need to use some rewrite rules, etc. So I need to set the web.config file.
So I need help to convert my .htaccess rules to web.config
This are the rules:
RewriteEngine On
RewriteRule ^box/camp(.+)?/?$ /_box/access_box_files/camp$1
RewriteRule ^box/merchant(.+)?/?$ /box/access_box_files/merchant$1
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url_tab=$1 [L,QSA]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
UPDATE
RewriteRule ^box/camp(.+)?/?$ /_box/access_box_files/camp$1
In this case I need that whenever the URL is "/box/camp" there is access to "/_box/access_box_files/cam" and that all the information that comes in front of "(.+)?" "$1" is included. Without changing the URL in the browser, or forwarding the user.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url_tab=$1 [L,QSA]
In this case, any information that comes in the URL must be collected in a variable.
For example:
https:// website dot com/tab1/tab2/tab3
I will get the information in: $_GET['url_tab'] which would have the information: "tab1/tab2/tab3".
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
In this case it is to force the URL to be loaded with https.
Thank you all very much.
Before changing something in my .htaccess links looked like this:
http://www.domain.tld/index.php
http://www.domain.tld/index.php?page=start
http://www.domain.tld/index.php?page=blog
Now I made a .htaccess with this code:
RewriteEngine on
RewriteRule ^index.php / [R,L,QSA]
Now the the links above looks like this in the browser address bar:
http://www.domain.tld
http://www.domain.tld/?page=start
http://www.domain.tld/?page=blog
That's nearly what I want, but how can I make it look like this?
http://www.domain.tld
http://www.domain.tld/start
http://www.domain.tld/blog
I tried so many different things, but mod_rewriting is not my intensity, yet.
I also want that search engines like Google uses the new URL (SEO).
Here's the solution to change links from http://www.domain.tld/index.php?page=blog to http://www.domain.tld/blog is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^\w+$ index.php?page=$0 [L]
RewriteCond %{THE_REQUEST} index\.php
RewriteCond %{QUERY_STRING} ^page=(\w+)$
RewriteRule ^index\.php$ /%1? [R=301,L]
and for links like: http://www.domain.tld/index.php?page=single_news&id=1&headline=This%20Is%20A%Headline
the solution is:
RewriteRule ^blog/(\d+)-([\w-]+)$ index.php?page=single_news&id=$1&headline=$2
After using this code, links looks like this: http://www.domain.tld/blog/2-this-is-a-headline
Mod Rewrite noob so pardon my ignorance but all I am trying to do is a simple query string removal
from: http://yourwebsite.com/x?section=y
to: http://yourwebsite.com/x/y
I am adding my mod rewrite rules in my .htaccess like this:
ErrorDocument 404 /404
Options +MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{QUERY_STRING} ^section=(.*)$ [NC]
RewriteRule ^(.*)$ /$1/%1? [R=301,L,NE]
The problem is that on visiting:
http://yourwebsite.com/x?section=y
My rule writes it back as:
http://yourwebsite.com/x.php/y
That .php in the pretty url is pretty darn ugly and I am struggling to get rid of it.
What is wrong in my mod rewrite rule?
Most likely you are looking for something like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)\/(.+)$ /$1.php?section=$2 [L]
RewriteCond %{QUERY_STRING} ^section=(.*)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1/%1 [R=301,L,NE]
It makes two rewrites:
a request x/y is internally rewritten to x.php?section=y
a request to x?section=y is redirected to x/y
Note that one is an internal rewrite, whilst the other redirects the browser to show a less 'ugly' url.
One hint: in case you can use the logging feature of apaches rewriting module (RewriteLog and RewriteLogLevel) this will offer you a wealth of detailed information on what is actually going on inside the rewrite process.
We're currently using folders inside webroot to store images and videos which we're loading in views (using the usual html image helper) that require log in.
How can I prevent outside visitors from just doing a site.com/img/photos/1.jpg url and having access to the images?
From what I understand I can't really use media views to render an image inside a proper view, and I can't figure out if there's a solution through htaccess manipulation.
Which is the best practise for this?
Perhaps choosing to go with a non-webroot folder would be best (although that would make it harder in the file-storing part)?
As poncha suggested, I tried editing the main .htaccess file into this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} !localhost
RewriteCond %{REQUEST_URI} ^app/webroot/img/
RewriteRule .* / [L,F]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
But the rewrite base line seems to be forbidding access to the whole site, and without it there seems to be no change in img access.
Edit 2:
Editing the htaccess inside webroot:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
# this RewriteCond is needed to avoid rewrite loops
RewriteCond %{REQUEST_URI} !^/app/webroot/
RewriteRule (.*) app/webroot/$1 [L,R]
RewriteCond %{HTTP_REFERER} !127.0.0.1
RewriteCond %{REQUEST_URI} ^/app/webroot/img/
RewriteRule .* - [L,F]
</IfModule>
This checks if Referer http header is set to something containing your domain, and denies access to img/ folder if not.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} !site.com
RewriteCond %{REQUEST_URI} ^img/
RewriteRule .* / [L,F]
Note: it is easy enough to "break" this protection if someone wants to steal your content, however, it does prevent hotlinking without the need to produce some sort of script that would pass thorugh all the images/videos to check if access should be granted.
EDIT:
In case your website is not in /, You have two options:
Change RewriteBase to reflect the base uri of the site (eg RewriteBase /app/webroot/)
Change RewriteCond to reflect the path from / (eg RewriteCond ^app/webroot/img/)
The second option is preferred in your case because you have other rules there
EDIT2:
In your case, the whole set should look like this:
RewriteEngine on
RewriteBase /
# this RewriteCond is needed to avoid rewrite loops
RewriteCond %{REQUEST_URI} !^/app/webroot/
RewriteRule (.*) app/webroot/$1 [L,R]
RewriteCond %{HTTP_REFERER} !localhost
RewriteCond %{REQUEST_URI} ^/app/webroot/img/
RewriteRule .* - [L,F]
I'm at a total loss trying to integrate a mod_rewrite in my existing page. Currently, I am building a new site from scratch, and there i have some nice clean url's such as:
http://www.example.nl/nl/example
The old site, running Cms made simple, has some not-rewritten url's that would need to be redirected to the new pages. Those url's look like this:
http://www.example.nl/index.php?page=cake-and-pie&hl=nl_NL
But shorter versions of that like:
http://www.example.nl/index.php?page=cake-and-pie
also work.
It took me a while to figure out that url's with parameters cannot simply be redirected with "Redirect 301", like i'd normaly do. So i tried some online mod_rewrite generators like this and this, but the rules outputted by those result only in 404 errors, (the redirect doesn't work at all).
My .htaccess file current looks like this:
RewriteEngine On
RewriteBase /
# remove .php;
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
The old pages would seize to exist.
How do i redirect the old pages to the new ones?
Thanks.
EDIT
RewriteCond %{QUERY_STRING} =page=pie
RewriteRule ^index\.php$ /nl/? [L,R=301]
RewriteCond %{QUERY_STRING} =page=pie&hl=nl_NL
RewriteRule ^index\.php$ /nl/? [L,R=301]
Seems to do the trick. This is of course manual for every url, but i only have a few.
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)/([^/]+)/?$ index.php?page=$1&hl=$2
RewriteRule ^([^/]+)/?$ index.php?page=$1 [QSA]