Apache htaccess rewriterule issue - php

I have the following rule in htaccess
RewriteRule ^noticias/?$ /index.php?view=pagination&ptype=noticias [L]
But the problem is that if I put some spaces in the URL, like this .. I can access the page normally.
http://gamesite.org/noticias /
I do not know what I do to block it.
without / the browser removes the spaces. but the problem is that I need this bar, because of paging on my site. Ex: /noticias/1/
Here is my htaccess file http://pastebin.com/gwtZGaGv

Related

PHP index.php File get other pages content from subdirectory location

How to get only an index.php in the server root directory and have all linked pages like /about.php /contact.php and so on located in a subdirectory like /pages/dist in the server? If someone visits example.com/about.php it get's the content from /pages/dist/[filename.php] ?
There are two ways that I have found to do it.
Using htaccess with regex:
Using rewrite engine, you link the base url to a folder like this:
RewriteEngine On
RewriteRule ^(?!index\.php)([\w-_]+)$ pages/dist/$1.php [QSA,L]
RewriteRule ^(?!index\.php)([\w-_]+\.php)$ pages/dist/$1 [QSA,L]
The second line is for files that end without the .php tag, and the third with.
What this does is if the user inputs in the url www.example.com/test.php it will show what is on the www.example.com/page/dist/test.php, even if the user strips the php tag, but if he inputs the index.php it will stay the same.
Using htaccess without regex:
This one is just for the simplicity, but if we where not to use regex it could look like this:
RewriteEngine ON
RewriteRule "test.php" "page/dist/test.php"
RewriteRule "test2.php" "page/dist/test2.php"
RewriteRule "test3.php" "page/dist/test3.php"
...
Please correct me if I'm wrong.

.htaccess URL rewriting - file loaded, but subsequent files don't work

I'm currently trying to use "pretty" URLs instead of using filenames through rewriting in the .htaccess.
This is what I would like to have:
site.com/dir/report1 shows site.com/dir/report.php
site.com/dir/report1/ shows site.com/dir/report.php
I don't want a redirect but just the file to be loaded without leaving the pretty URL.
The first one works, but with the all calls to e.g. stylesheets now have "report1/" as base directory and thus can't be found.
Is there any way around this?
Here's my code:
RewriteEngine On
RewriteBase /dir/
RewriteRule ^report1/?$ report.php [NC,PT,L]
With help from #arkascha I've been able to solve this by redirecting all requests to "/report1/" to "/report1". This is my code:
RewriteEngine On
RewriteBase /dir/
RewriteRule ^report1/$ report1[R=301]
RewriteRule ^report1$ report.php [L]

URL rewrite to home page using .htaccess

I am looking to serve /myweb/index.php file as the default landing page when someone tries to access the domain using myweb.com. I tried using several combinations inside .htaccess but failed to achieve the result.
Another part of the requirement is that other URL redirection shouldn't have a problem due to this particular rewrite url, in other words rest of the url shouldn't look inside /myweb/ to serve images and whatnot.
Right now I have these inside my .htaccess
RewriteRule ^$ myweb/index.php [L] (This does not)
RewriteRule ^shops/(.*)$ /myweb/shops/$1 [L] (This works)
RewriteRule ^content/(.*)$ /myweb/content/$1 [L] (This works)
.htaccess file is placed at /var/lib/tomcat7-cms/webapps/ROOT
Anyhelp regarding this would be much appreciated.

How to ignore a folder from beautiful URL using htaccess

I am developing a website, assuming example.com and a blog located at example.com/blog.
The website language is PHP (I designed and programmed it by myself), and I used wordpress as the blog.
I tried to use beautiful URL in main part of the website to convert example.com/services.php to example.com/services by means of this code in htaccess:
# BEAUTIFUL URL
RewriteBase /
DirectorySlash Off
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
# rewrite /dir/file?query to /dir/file.php?query
RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 [L,T=application/x-httpd-php]
The main part of the website works fine. But when I try to load the blog, I must add a trailing slash at the end of the URL to make it work.
http://www.example.com/blog >> 403 Forbidden error
http://www.example.com/blog/ >> Loads correctly
I tried to add trailing slash after URLs using htaccess, But I dont know is this the best solution or not? Could anyone suggest a way to ignore blog folder to be effected by beautiful url?
I simple way to handle this is to create a file called blog.php which simply redirects to example.com/blog/.
RewriteCond %{REQUEST_URI} !^/blog
above the RewriteRules.
Not tested it, I think it should work

Remove in between folder structure from the url in a php website

I have a php website having following folder structure (basic structure).
project_name
app
controller
model
view
css
js
img
index.php
So when I view index.php in WAMP the url is http://localhost/project_name/
But when I go inside the site (eg. login.php which resides under view folder) url is like this. http://localhost/project_name/app/view/login.php
I found that using .htaccess we can change the urls. So I tried this (in .htaccess).
RewriteEngine on
RewriteBase /
Redirect 301 /project_name/app/view/login.php /project_name/login.php
RewriteRule ^/project_name/login.php$ /project_name/app/view/login.php [L]
Now url is http://localhost/project_name/login.php It is correct. But it seems php does not use the original link to grab the file (ie. from /project_name/app/view/login.php) but from here /project_name/login.php
So it throws 404 error.
What should I change? Please help me, i am just trying to hide /app/view/ part from the url so that user won't see my folder structure. I have read about various ways of doing that for about 9hrs today but still couldn't get anything working correctly.
Hope my question is clear enough. Any help is greatly appreciated!
URI's passed through rewrite rules in an htaccess file has the leading slash removed, so your rule:
RewriteRule ^/project_name/login.php$ /project_name/app/view/login.php [L]
won't ever match anything because of ^/. Also, since you are rewriting to a URI that matches a previous redirect, your browser will see a redirect loop and say that it's not redirecting properly. You'll need to match against the actual request and not the URI:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /project_name/app/view/login\.php
RewriteRule ^ /project_name/login.php [L,R=301]
RewriteRule ^/?project_name/login.php$ /project_name/app/view/login.php [L]

Categories