使用.htaccess隐藏.php扩展名

时间:2018-04-16 22:15:30

标签: .htaccess

我有一个小网站,其中包含两个页面,主页(index.php)和预订(booking.php)页面,我想隐藏其扩展名。

我在.htaccess文件中使用以下代码,但它似乎不能像我想的那样工作。

我想做的只是make booking.php显示为example.com/booking,我的index.php始终使用example.com。

RewriteEngine on
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([a-z0-9_-\s]+)/?$ /$1.php [NC,L]
Redirect /booking.php /booking
Redirect /index.php /

2 个答案:

答案 0 :(得分:0)

我会使用以下内容将所有与资产无关的请求重定向到index.php:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [qsa]

然后在index.php文件中,执行以下操作:

if ($_SERVER["REQUEST_URI"] == "/")
{
    //index page code
} elseif ($_SERVER["REQUEST_URI"] == "/booking")
{
    //booking page code
} else
{
    header("HTTP/1.0 404 Not Found");
}

这样,在不触及.htaccess的情况下添加其他页面相对容易,您可以将任何包含的文件等保留在您的webroot之外。

行" RewriteCond%{REQUEST_FILENAME}!-d"和" RewriteCond%{REQUEST_FILENAME}!-f"检查是否存在所请求的文件,例如一个图像 在将请求发送到index.php之前,这实际上是一个后备。

答案 1 :(得分:0)

您正在将public void ImageOnClick(View v) { Log.i("OnClick","True"); AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle(R.string.AlertTitle); builder.setItems(Items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (Items[which].equals("Camera")){ Intent CameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); if(CameraIntent.resolveActivity(getPackageManager()) != null){ startActivityForResult(CameraIntent, CAMERA_CODE); } }else if (Items[which].equals("Gallery")){ Log.i("GalleryCode",""+GALLERY_CODE); Intent GalleryIntent = null; GalleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); GalleryIntent.setType("image/*"); GalleryIntent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(GalleryIntent,GALLERY_CODE); } } }); builder.show(); } (重定向指令)与mod-alias(RewriteRule指令)混合。这是两个不同的apache模块,具有不同的运行时行为。您可以使用以下mod重写规则:

mod-rewrite
相关问题