hmm I have a problem with my .htaccess, I wanted to rewrite this:
http://domain.com/a/thread-103518.html#103518
to this
http://domain.com/a/103518
In other words... I want to delete the "thread-" and everything after the "." The /a/ must be variable... there are other forums with /bla/ and /code/
Do you have a clue for me? Thank you
My .htaccess right now:
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond
%{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index.php RewriteRule
^index.php/?(.*) /$1 [L,R=301]
Add this to the .htaccess file in your DOCUMENT_ROOT
RewriteEngine On
RewriteRule ^([^/]+)/thread-(\d+) $1/$2? [DPI,L,R]
Tested in Apache 2.2 and 2.4 :)
This assumes that mod_rewrite is both installed and activated for htaccess files.
If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All
Related
I currently have a CRUD that has the following file structure:
The index.php file redirects to the add-product.php, delete.php and other files. The problem is, the URL looks like: myapp.com/resources/views/add-product but I want it to look like myapp.com/add-product. I found other StackOverflow posts similar to this, but it redirects to the desired link, instead of redirecting to the correct link but showing the desired one, and because of that the site doesn't work ("The requested URL was not found on this server"). The .htaccess file looks like this:
Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^resources/(.*)$ /$1 [L,NC,R]
I'm also using the file to be able to remove the .php from the URLs. The last line is the one handling the wrong redirection. How can I go about this? Thank you for your time.
You may use it like this:
Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteBase /myapp/
# external redirect to remove /resources/views/ from URLs
RewriteRule ^resources/views/(.+)\.php$ $1 [L,NC,R=302]
# internal rewrite to add /resources/views/ and .php to show content
RewriteCond %{DOCUMENT_ROOT}/myapp/resources/views/$1.php -f
RewriteRule ^(.+?)/?$ resources/views/$1.php [END]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
I have a .htaccess file inside an existing project folder. The code for the .htaccess file is below.
# Use PHP54 Single php.ini as default
#Options +FollowSymLinks
#Options +Indexes
RewriteEngine On
RewriteBase /
#RewriteRule ^users/([A-Za-z0-9_]+)$ users/index.php [NC,L]
#RewriteRule ^([A-Za-z0-9_]+)$ users/$1 [NC,L]
#RewriteRule ^ - [L]
#RewriteCond %{DOCUMENT_ROOT}/users/$1 -f
#RewriteRule .* users/$1 [L]
#Options -Indexes
#RewriteEngine On
#RewriteRule ^[!/.]*([A-Za-z0-9]+)/?$ /index.php?id=$1 [NC,L]
#<FilesMatch "\.(?i:vcf)$">
# ForceType application/octet-stream
# Header set Content-Disposition attachment
#</FilesMatch>
# If requested resource exists as a file or directory, skip next two rules
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [S=2]
#
# Requested resource does not exist, do rewrite if it exists in /users
RewriteCond %{DOCUMENT_ROOT}/users/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/users/$1 -d
RewriteRule (.*) users/$1 [L]
#
# Else rewrite requests for non-existent resources to /index.php
RewriteRule (.*) users/index.php?q=$1 [L]
So what happens it, when I try to open the project in url at localhost
http://localhost/projectfolder
it opens the index file without any javascript and css. Also the links to other pages do not open and the error message shows as -"Object not found". So I can only view index.php with out css and js files.
But when I remove / rename the .htaccess file, all works fine. So there must be something in this file which blocks all css, js files. So I am not asking for all the rules of writing a .htaccess file, I just need to know which line of code is blocking the files and how can I edit them, so it will not block any file wile working in localhost.
Note:- This question I am asking only for my knowledge. As I have written, if I remove the file then its working fine. But still I want to know which line of code is blocking the access to files. Thanks.
I suspect this rule is not doing what you think it should be doing:
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [S=2]
Replace this with:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
Also you might be using relative links for css/js/images which will also cause 404 after rewriting to /users/. You have 2 options to fix that:
use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
You can try adding this in your page's HTML header:
<base href="/" />
I have this URL:
http://www.example.com/subf/subf2/download.php?token=6309838552568
It is possible to rewrite this url into this url?
http://www.example.com/subf/subf2/download/6309838552568
Note: If someone types a URL like this:
http://www.example.com/subf/subf2/download/6309838552568/sbuf3/subf4
i want to show them an error page.
Answer below
Hint: Include files like JS and CSS need to be refer in absolute paths.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT/subf/subf2/ directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /subf/subf2/
RewriteRule ^(download)/(\w+)/?$ $1.php?token=$2 [L,QSA,NC]
# to take care of css, js, images
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^download/(.+)$ /subf/subf2/$1 [L,R=301,NC]
The title explains the basics, now, here comes the problem:
I have many files that look like this:
<?php include 'mynavbar.php';
include 'myheader.php';
include 'myfirstline.php';
include 'mybuttons.php';
include 'myadvert.php';
include 'myimg.php';?>
And I can't edit all the files and remove the .php. How can I not show the .php in the address bar but still be able to include the pages by using the .php ending
Following code should work for you in .htaccess file to hide .php extension:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# to make `/path/index.php` to /path/
RewriteCond %{THE_REQUEST} ^GET\s(.*/)index\.php [NC]
RewriteRule . %1 [NE,R=301,L]
RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [NE,R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Also remember that these rules will have NO impact in your php includes eg: include 'myheader.php';
It is because those includes are processed by php itself and don't go through Apache.
The following RewriteRules will assume the PHP extension for any file that is not otherwise matched. It should go at the bottom of your RewriteRules in your .htaccess file.
# Force PHP extension if not a directory
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^(.*)$ - [L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^((.*/)*[^./]+)/*$ $1.php [L]
This is for URL Rewriting. As far as include statements, you still need the PHP extension as these are physical file paths. If you need to so something fancy there, you should look into symbolic links.
You are not taking the correct path for fixing the problem you trying to solve.
If I understood well, what you want to do is having an url like :
http://example.com/my/url
instead of (for example):
http://example.com/my/url.php
If this is the case, what you should look for is rewrite url
The path to my django site is: staging.ninjawebsite.com/clients/project/.
I want that to be treated as the base url. The django project is in the public_html/clients/project subfolder. Everything seems to be working fine but all links to say /city/ should go to staging. ninjawebsite.com/clients/project/city/ but it goes to staging.ninjawebsite.com/city/.
Here is my .htaccess file:
AddHandler fcgid-script .fcgi
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]
RewriteCond %{REQUEST_URI}!(django.fcgi)
RewriteRule ^(.*)$ /clients/project/django.fcgi/$1 [QSA,L]
Somebody please help me! Thanks.