htaccess | How to redirect using RewriteRule ? Simple - php

I have this URL pointing my web site:
http://www.mysite.ext/.htaccess.aspx-->/
and I like to redirected to
http://www.mysite.ext/
but I can't.
In my .htaccess file I have enter this rule:
RewriteRule ^(.htaccess(.+))/?$ http://www.mysite.ext/? [R=301,L]
but doesn't work
also I have try the following:
RewriteRule ^(\.htaccess\.aspx(.*))/?$ http://www.mysite.ext/? [R=301,L]
but still no luck. I don't know if that helps, but the site is based on PHP.
Any idea please ?
Is it realy so hard ?
Can somebody to help me please ?

Not sure how you are you getting requests for /.htaccess.aspx and why you want to redirect them.
However keep in mind that Apache configs usually block access to .htaccess using directive like below:
<FilesMatch "^\.ht">
Order allow,deny
Deny from all
Satisfy All
</FilesMatch>
That throws 403 (Forbidden) error for any request that starts with /.ht.
Workaround:
Have a custom handler for 403:
ErrorDocument 403 /errorPage403.php
and have this redirect rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\.htaccess\.aspx [NC]
RewriteRule ^ /? [L,R]

Related

htaccess - Redirect to error page if user attempts direct file access?

I have a website where the main directory contains a few files i don't want to be directly accessed using urls like this:
http://website.com/somefile1.ext1
http://website.com/somefile2.ext1
http://website.com/somefile1.ext2
http://website.com/somefile2.ext2
Currently they download if you enter the urls in a browser. Instead i would like to redirect them to a error page like this:
http://website.com/404
I found this snippet online but it doesn't work, it also doesn't redirect:
<Files ~ "\.(ext1)$">
Order allow,deny
Deny from all
</Files>
What would the correct .htaccess code be to do this task?
Edit:
I redirect site.com/script.php etc to site.com/script with this code:
RewriteEngine On
# turn on the mod_rewrite engine
RewriteCond %{REQUEST_FILENAME}.php -f
# IF the request filename with .php extension is a file which exists
RewriteCond %{REQUEST_URI} !/$
# AND the request is not for a directory
RewriteRule (.*) $1\.php [L]
# redirect to the php script with the requested filename
Try it like this I didn't tried it for now,
RewriteEngine On
RewriteCond %{REQUEST_URI} \.ext\d$
RewriteRule ^ 404 [R=301]
Try
<FilesMatch "somefile\.ext$">
Deny from all
</FilesMatch>
You don't need to use the Order directive if you want to deny access from all.

.htaccess rule error for redirecting from a homepage form submit

I have a top level domain that has a form in it, when I submit the form I get the error that it's no found (it's looking for it in the wrong place.)
the original locations of the index is here: www.tld.com/tld/src/index.html
the php file that handles the form into the DB is here: www.tld.com/tld/lib/phpfile.php
I am using this .htaccess rule to send my homepage to www.tld.com:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/tld/src/
RewriteRule ^(.*) /tld/src/$1 [L]
Obviously, the redirection is an error: The requested URL /tld/src/lib/phpfile.php was not found on this server. since the file exists in www.tld.com/tld/lib/phpfile.php
My current and only settings except for the above are:
Options All -Indexes
<Files .htaccess>
deny from all
</Files>
And that's it.
I am new to .htaccess apache rulesets and I have no idea how to fix this. Thank you.
You can tweak your exception condition:
RewriteEngine On
RewriteBase /tld/
RewriteCond %{REQUEST_URI} !/(src|lib)/ [NC]
RewriteRule ^(.*) src/$1 [L]
Above condition will skip any request that starts with /tld/lib/ or /tld/src/ thus allowing your form to be routed to correct php file in /tld/lib/ directory.

URL rewrite rule is not working in php

i am using godaddy hosting and addon domain. i want to make my long url to short url.
for example my url is follwing:
www.example.com/events/landing_event.php?url=text-first
and i want following url:
www.example.com/events/text-first
for this i am using following rewrite url in .htaccess file
RewriteRule ^([a-zA-Z0-9-/]+)$ events/landing_event.php?url=$1 [L]
RewriteRule ^([a-zA-Z0-9-/]+)/$ events/landing_event.php?url=$1 [L]
but this rewrite rule is not working.
so please help how can i make short url.
Use this source after you make sure RewriteEngine Enabled and On
RewriteEngine On
RewriteRule ^events/([^/]*)$ /events/landing_event.php?url=$1 [L]
The original URL:
http://www.example.com/events/landing_event.php?url=text-first
The rewritten URL:
http://www.example.com/events/text-first
Updated with 500 Internal Server Error
Your code is guaranteed to generate 500 internal server error because it is causing infinite looping. Reason is that your matching URI pattern is: ^events/([^/]*)$
Which matches your URLs before and after rewrites. And once it reaches max allowed internal rewrite limit Apache throws 500 internal server error and bails out.
Change your code to this
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^events/([^/]*)$ /events/landing_event.php?url=$1 [L,QSA,NC]
Try this code :
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /events/landing_event.php?url=$1 [L]
Add this in your .htaccess file
You can see there are many ways to achieve that goal.
Also
RewriteEngine On
RewriteRule ^events/([^/]*)\.html$ /events/landing_event.php?url=$1 [L]
would do the job. So basicly, how does the full scope of your project urls look like?
Like do you always want to print the parameters? If yes, also the name sometimes or also the value?
The newer versions of apache requires you to change AllowOverride on /etc/apache2/apache2.conf
from
AllowOverride None
to
AllowOverride All

PHP - how to prevent .php files from direct access

i have following folders:
mywebsite/en/index.php
mywebsite/en/product/info.php
mywebsite/en/tags/tags.php
mywebsite/en/games/list.php
my site is running on friendly url, top mentioned files accessable from following friendly urls
mywebsite/en/?pageno=1
mywebsite/en/product/Nike-glove
mywebsite/en/tags/Nike
mywebsite/en/games/ice-sports
i have some examples, but nothing work for me like...i want to prevent direct access to .php files..i want to do that with htaccess...can i?
thanks and kind regards
Reelmark
add 404 page and try following...
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\.php[/?\ ]
RewriteRule .*\.php$ 404.php [L]
Not sure exactly sure what you are trying to accomplish, but this will prevent direct access of your php files:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[^\ ]+\.php($|\ )
RewriteRule \.php$ / [F,L]
Though I'm guessing you really want to redirect to the friendly urls when accessing the php files directly.
<Files ~ "\.php$">
Order allow,deny
Deny from all
</Files>
The solution I've settled on is based on #Fou's answer, but I've modified it to handle the case of /script.php/oops and opted to rewrite the response code as 404 instead of using a temporary redirect:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\.php[/?\ ]
RewriteRule .* - [R=404]
just add this into your htaccess file... give it a try :) hope it useful
RewriteEngine On
IndexIgnore *

mod_rewrite user id error

I'm new to rewriting urls and wanted to know how i would rewrite
From this
/profile/4
To this
/profile.php?id=4
I have this rule so far
RewriteRule ^profile/([0-9]+)/?$ profile.php?id=$1
but it displays this in the browser
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
It displays /profile/4/index.php in the browser address bar which is incorrect.
.htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
Options +FollowSymlinks
RewriteEngine on
#ErrorDocument 404 /test.php
DirectoryIndex test.php
RewriteRule settings editProfile.php
RewriteRule update update.php
RewriteRule home test.php
RewriteRule ^profile/([0-9]+)/?$ profile.php?id=$1
You are pretty much there. Try using this instead
RewriteRule ^profile/(.*)$ profile.php?id=$1
I found this good free resource online to help you test your rewrite rules before you post them to your live site. This may help for the future. It looks like your rule should work as you have it posted though. The problem must be somewhere else.
Your rule is just fine so there's a redirection somewhere conflicting with it. I suppose that you don't have a real directory at /profile/4 so they main candidate left is mod_negotiation. Try this:
Options -MultiViews

Categories